如何从 javascript Intl.DateTimeFormat 获取时区缩写 [英] How to get timezone abbreviations from javascript Intl.DateTimeFormat

查看:44
本文介绍了如何从 javascript Intl.DateTimeFormat 获取时区缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ECMA-402 国际 API 来获取不是本地时区的时区中的时区缩写(服务器时区是 UTC).我知道其他方法来获得这个.我试图了解国际 API 的局限性并充分利用它.我可以获得完整的时区名称并自己映射它,但是由于缩写在 IANA tz 数据库中,并且国际 API 应该基于此,它似乎应该能够生成它们,这让我觉得我是做错事了.

I am trying the ECMA-402 International API to get the timezone abbreviation in a timezone that is not the local timezone (server timezone is UTC). I know other ways to get this. I am trying to understand the limitations of and make best use of the International API. I can get the full timezone name and map it myself, but as the abbreviations are in the IANA tz database and the International API is supposed to be based on this, it seems it should be able to produce them, which makes me think I am doing something wrong.

我有以下代码:

    const fmt = new Intl.DateTimeFormat('en', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: 'numeric',
        fractionalSecondDigits: 3,
        hour12: false,
        weekday: 'short',
        timeZoneName: 'short',
        timeZone: 'Pacific/Auckland' 
    });

    const now = new Date();
    console.log(fmt.format(now));

    const fmt2 = new Intl.DateTimeFormat('en', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: 'numeric',
        fractionalSecondDigits: 3,
        hour12: false,
        weekday: 'short',
        timeZoneName: 'short',
        timeZone: 'America/Los_Angeles' 
    });

    console.log(fmt2.format(now));

在节点 12.16.1 和 Firefox 73.0.1 中,这会产生如下输出:

In both node 12.16.1 and Firefox 73.0.1 this produces output like the following:

Wed, 04/08/2020, 18:14:50 GMT+12
Tue, 04/07/2020, 23:14:50 PDT

America/Los_Angeles 时区按预期获得时区缩写,但太平洋/奥克兰时区没有.IANA tz 数据库有 Pacific/Auckland 的缩写,并且由操作系统 (Debian Linux) 生成.

The America/Los_Angeles timezone gets the timezone abbreviation as expected but the Pacific/Auckland timezone does not. The IANA tz database has the abbreviations for Pacific/Auckland and the operating system (Debian Linux) produces them.

有什么我可以做的不同的事情来从国际 API 中获取缩写吗?或者这只是最先进的技术?

Is there anything I can do differently to get the abbreviations from the International API? Or is this simply the state of the art?

我注意到 luxon 和 date-fns-tz 都依赖于国际 API,而且它们也无法生成 Pacific/Auckland 的缩写.

I note that both luxon and date-fns-tz depend on the International API and they also fail to produce the abbreviations for Pacific/Auckland.

推荐答案

ECMAScript 国际化 API 的大多数实现从 Unicode CLDR,不是来自 IANA.IANA 中的缩写词只有英文,而且许多缩写词在最近几年被删除,发现它们是在那里发明的.

Most implementations of the ECMAScript Internationalization API derive time zone abbreviation strings from Unicode CLDR, not from IANA. Abbreviations in IANA are English-only, and many have been removed in recent years, where it was found they had been invented.

遗憾的是,CLDR 数据集中实际包含的时区缩写很少.

Unfortunately, there are very few time zone abbreviations actually included in the CLDR data set.

一般来说,时区缩写很难达成一致.在某些情况下,同一时区使用多个缩写.一些文化转而使用英语缩写,而另一些文化则在自己的语言中使用自己的缩写,而许多文化根本不使用它们.

In general, time zone abbreviations are difficult to get agreement on. In several cases, more than one abbreviation is used for the same time zone. Some cultures switch to English abbreviations, while others have their own in their own languages, and many cultures simply don't use them at all.

考虑到所有这些,我认为您仍然应该使用 Intl API 提供的输出.在有缩写的地方,你会有它们,在没有缩写的地方,你会有一个数字偏移量.是的 - 这是目前的技术水平.

Given all this, I'd say you should still use the output given by the Intl API. Where abbreviations are available you'll have them, and where they aren't you'll have a numeric offset. Yes - that's the current state of the art.

这篇关于如何从 javascript Intl.DateTimeFormat 获取时区缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆