javascript - 使用 luxon 检测时区缩写

标签 javascript datetime timezone momentjs luxon

具有短时区缩写的时刻时区结果,例如

moment.tz([2012, 0], 'America/New_York').format('z');    // EST
moment.tz([2012, 5], 'America/New_York').format('z');    // EDT

有没有类似的方法可以使用 luxon 来实现

我试过offsetNameShort ,但是,结果为 GMT+5:30对于像 "2020-05-23T13:30:00+05:30" 这样的日期

DateTime.fromISO(""2020-05-23T13:30:00+05:30"").toFormat('z')也不起作用

有什么方法可以删除 +5:30时区从格式?

最佳答案

点评 Luxon's table of formatting tokens .你想要ZZZZ为缩写命名的偏移量。
例子:

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 获取数据。 .
在 CLDR 中,许多名称和缩写被指定为特定于给定语言环境,而不是在全局范围内使用。 Europe/London 也会发生同样的事情。获取 GMT+1而不是 BST除非语言环境是 en-GB . (我个人不同意这一点,但这就是目前的实现方式。)

关于javascript - 使用 luxon 检测时区缩写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62019992/

相关文章:

javascript - 从谷歌地图中删除折线

c - 想要使用 struct tm 执行日期/时间值操作

python - Matplotlib 的 get_ticklabels 不适用于自定义字符串标签

python - 在Python的帮助下转换时区的输出格式

c# - 按州/省和国家/地区获取时区?

javascript - 在网络应用程序中识别用户的最佳方法

javascript - 使用 Python 和 Selenium Webdriver 抓取 javascript

php - 用户将超链接和图片粘贴到文本区域

PostgreSQL:如何测试时间戳是否有时区?

python - 使用 strptime() 解析日期/时间字符串时如何保留时区?