实现说明
循环只出现在 for (const [locale, options] of configs),它的每次迭代处理一个元素。
演示用的数据放在这几个模块级绑定中:configs。
使用要点
- 导入面(如果有的话)全在标准库内,随语言本身一起版本化。
- 演示输出是带标签的(
formatted:),每个操作都会留下自己的一行标注。
常见问题
这段代码会引入外部包吗?
不涉及外部包,每个调用都落在内建 API 上。 文件的状态由几个模块级绑定持有:configs。
核心语句
const fmt = new Intl.DateTimeFormat(locale, options);
代码示例
const configs = [
['en-US-u-hc-h23', { dateStyle: 'short', timeStyle: 'short', timeZone: 'UTC' }],
['th-TH-u-ca-buddhist', { dateStyle: 'medium', timeZone: 'Asia/Bangkok' }],
['ar-EG-u-nu-arab', { dateStyle: 'medium', timeZone: 'Africa/Cairo' }],
];
for (const [locale, options] of configs) {
const fmt = new Intl.DateTimeFormat(locale, options);
const resolved = fmt.resolvedOptions();
console.log(
resolved.locale,
'| tz:', resolved.timeZone,
'| cal:', resolved.calendar,
'| num:', resolved.numberingSystem,
'| hc:', resolved.hourCycle ?? 'none',
);
console.log(' formatted:', fmt.format(new Date('2026-11-24T14:05:00Z')));
}