bri*_*ant 5 javascript timezone datetime momentjs luxon
时刻时区结果带有短时区缩写,例如
moment.tz([2012, 0], 'America/New_York').format('z'); // EST
moment.tz([2012, 5], 'America/New_York').format('z'); // EDT
Run Code Online (Sandbox Code Playgroud)
是否有类似的方法可以使用 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从格式中删除时区?
Review Luxon's table of formatting tokens. You want ZZZZ for the abbreviated named offset.
Examples:
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"
Run Code Online (Sandbox Code Playgroud)
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.
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.)