Intl.Locale.prototype.getTimeZones()
Intl.Locale 实例的 getTimeZones() 方法返回此区域设置支持的时区列表。
¥The getTimeZones() method of Intl.Locale instances returns a list of supported time zones for this locale.
注意:在某些浏览器的某些版本中,此方法被实现为名为
timeZones的访问器属性。但是,由于每次访问时都会返回一个新数组,因此现在将其实现为一种方法,以防止出现locale.timeZones === locale.timeZones返回false的情况。检查 浏览器兼容性表 了解详细信息。¥Note: In some versions of some browsers, this method was implemented as an accessor property called
timeZones. However, because it returns a new array on each access, it is now implemented as a method to prevent the situation oflocale.timeZones === locale.timeZonesreturningfalse. Check the browser compatibility table for details.
语法
参数
返回值
¥Return value
表示关联 Locale 支持的时区的字符串数组,其中每个值都是 IANA 时区规范名称,按字母顺序排序。如果区域设置标识符不包含区域子标签,则返回值为 undefined。
¥An array of strings representing supported time zones for the associated Locale, where each value is an IANA time zone canonical name, sorted in alphabetical order. If the locale identifier does not contain a region subtag, the returned value is undefined.
请注意,虽然 IANA 数据库会不时更改,但 Unicode CLDR 数据库(浏览器使用)保留旧时区名称以保证稳定性。例如,以下是一些值得注意的名称更改:
¥Note that while the IANA database changes from time to time, the Unicode CLDR database (which browsers use) keeps old time zone names for stability purposes. For example, here are a few notable name changes:
| 当前 IANA 名称 | CDLR 数据库 |
|---|---|
America/Argentina/Buenos_Aires |
America/Buenos_Aires |
Asia/Kolkata |
Asia/Calcutta |
Asia/Ho_Chi_Minh |
Asia/Saigon |
Europe/Kyiv |
Europe/Kiev |
有些浏览器(Firefox)会覆盖这些旧名称,而其他浏览器则不会(Safari 和 Chrome)。有关更多信息,请查看 CLDR 数据库。(如果不同,IANA 名称将标记为 "_iana"。)有 TC39 中为正确处理这些规范标识符所做的努力,其中还包含指向相关 CLDR 问题的链接。
¥Some browsers (Firefox) override these legacy names, while others don't (Safari and Chrome). For more information, check the CLDR database. (IANA names are marked with "_iana", if different.) There is an effort in TC39 to properly handle these canonical identifiers, which also contains links to related CLDR issues.
示例
获取支持的时区
¥Obtaining supported time zones
列出给定 Locale 支持的时区。
¥List supported time zones for a given Locale.
const arEG = new Intl.Locale("ar-EG");
console.log(arEG.getTimeZones()); // ["Africa/Cairo"]
const jaJP = new Intl.Locale("ja-JP");
console.log(jaJP.getTimeZones()); // ["Asia/Tokyo"]
const ar = new Intl.Locale("ar");
console.log(ar.getTimeZones()); // undefined
规范
| Specification |
|---|
| Intl Locale Info Proposal # sec-Intl.Locale.prototype.getTimeZones |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also
Intl.Locale- 维基百科上的 IANA 时区数据库