← 中文资源

简体中文版本

用 Intl.DateTimeFormat.resolvedOptions 查验最终区域设置

调用 Intl.DateTimeFormat 实例的 resolvedOptions() 读取运行期实际生效的 timeZone、calendar、numberingSystem 与 hourCycle,核验区域扩展标签回退结果。

阅读英文原文

运行结果

以下输出由构建管线在沙箱中真实运行该示例捕获。

en-US-u-hc-h23 | tz: UTC | cal: gregory | num: latn | hc: h23
  formatted: 11/24/26, 14:05
th-TH-u-ca-buddhist | tz: Asia/Bangkok | cal: buddhist | num: latn | hc: none
  formatted: 24 พ.ย. 2569
ar-EG-u-nu-arab | tz: Africa/Cairo | cal: gregory | num: arab | hc: none
  formatted: ٢٤‏/١١‏/٢٠٢٦

实现说明

循环只出现在 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')));
}