使用luxon检测时区缩写 [英] Detect timezone abbreviation using luxon
问题描述
时区缩写时的时区结果,例如
Moment timezone results with short timezone abbreviations, e.g
moment.tz([2012, 0], 'America/New_York').format('z'); // EST
moment.tz([2012, 5], 'America/New_York').format('z'); // EDT
有没有类似的方法可以使用luxon来实现?
Is there a similar way we can achieve that using luxon
我尝试了 offsetNameShort
,但是结果为 GMT + 5:30
日期,例如 2020-05-23T13:30:00 + 05:30
I tried offsetNameShort
, but, it results to GMT+5:30
for a date like "2020-05-23T13:30:00+05:30"
类似 DateTime.fromISO( 2020-05-23T13:30:00 + 05:30)。toFormat('z')
也不起作用
是否可以从格式中删除 +5:30
时区?
Is there a way we can remove the +5:30
timezone from the format?
推荐答案
评论 Luxon的格式标记表。您需要 ZZZZ
作为缩写的偏移量。
Review Luxon's table of formatting tokens. You want ZZZZ
for the abbreviated named offset.
示例:
DateTime.fromObject({year: 2012, month: 1, zone: 'America/New_York'})
.toFormat('ZZZZ') //=> "EST"
DateTime.fromObject({year: 2012, month: 6, zone: 'America/New_York'})
.toFormat('ZZZZ') //=> "EDT"
DateTime.local()
.toFormat('ZZZZ') //=> "PDT" (on my computer)
DateTime.fromISO("2020-05-23T13:30:00+05:30", {zone: 'Asia/Kolkata', locale: 'en-IN'})
.toFormat('ZZZZ') //=> "IST"
在最后一个注释中,您还必须指定 en -IN
作为语言环境,以获取 IST
。否则,您将获得 GMT + 05:30
,除非系统区域设置已经 en-IN
。这是因为Luxon依赖浏览器的国际化API,而后者又从 CLDR 中获取数据。
Note in that last one, you also have to specify en-IN
as the locale to get IST
. Otherwise you will get GMT+05:30
, unless the system locale is already en-IN
. That is because Luxon relies upon the browser's internationalization APIs, which in turn takes its data from CLDR.
在CLDR中,许多名称和缩写被指定为特定于给定的语言环境,而不是在全球范围内使用。 欧洲/伦敦
获得 GMT + 1
而不是 BST
,除非语言环境是 en-GB
。 (我个人不同意这一点,但是目前是这样实现的。)
In CLDR, many names and abbreviations are designated as being specific to a given locale, rather than being used worldwide. The same thing happens with Europe/London
getting GMT+1
instead of BST
unless the locale is en-GB
. (I personally disagree with this, but that is how it is currently implemented.)
这篇关于使用luxon检测时区缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!