Javascript 读取完整时区名称
提供了一系列现成的强大功能 其中一个是通过插件显示时区的字符串缩写,如Javascript 读取完整时区名称,javascript,typescript,timezone,momentjs,Javascript,Typescript,Timezone,Momentjs,提供了一系列现成的强大功能 其中一个是通过插件显示时区的字符串缩写,如CET: const locationName = moment.tz.guess(true) // "Europe/Berlin" const timezoneInitials = moment.tz(locationName).format('zz') // "CET" 是否可以从矩对象获取CET(即中欧时间)的完整时区名称字符串? 如果没有,还有什么选择 到今天为止,这
CET
:
const locationName = moment.tz.guess(true) // "Europe/Berlin"
const timezoneInitials = moment.tz(locationName).format('zz') // "CET"
是否可以从矩对象获取CET(即中欧时间
)的完整时区名称字符串?
如果没有,还有什么选择
到今天为止,这种解析还没有提供/记录。从类似的列表中映射和重复数据消除似乎也不是最佳选择。矩并没有直接提供此功能 但是,大多数现代浏览器都支持ECMAScript国际化API,允许您编写如下函数:
function getTimeZoneLongName(date, locale, timeZone) {
const f = Intl.DateTimeFormat(locale, { timeZone, timeZoneName: 'long' });
return f.formatToParts(date).find(x => x.type === 'timeZoneName').value;
}
示例用法:
// current date/time, current locale and time zone
getTimeZoneLongName(new Date()) //=> "Pacific Standard Time"
// current date/time, specific locale, current time zone
getTimeZoneLongName(new Date(), "fr") //=> "heure normale du Pacifique nord-américain"
// current date/time, specific locale, specific time zone
getTimeZoneLongName(new Date(), "fr", "Asia/Tokyo") //=> "eure normale du Japon"
// specific date/time, current locale, specific time zone
getTimeZoneLongName(new Date(2020, 5, 1), undefined, "Europe/London") //=> "British Summer Time"
(示例输出来自Windows 10上的Chrome 78,英语,美国太平洋时区)
请注意,这会给出标准时间或夏令时的名称,具体取决于给定日期的生效日期。目前没有检索通用名称的方法
换句话说,对于英语和
Europe/Berlin
,它将返回“中欧标准时间”
或“中欧夏季时间”
,但不仅仅是“中欧时间”
,,我不这么认为,因为他们建议您可以通过在