11

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

Is there a similar way we can achieve that using luxon

I tried offsetNameShort, but, it results to GMT+5:30 for a date like "2020-05-23T13:30:00+05:30"

Something like DateTime.fromISO(""2020-05-23T13:30:00+05:30"").toFormat('z') doesn't work either

Is there a way we can remove the +5:30 timezone from the format?

brijesh-pant
  • 967
  • 1
  • 8
  • 16

2 Answers2

20

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"

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.)

matewka
  • 9,912
  • 2
  • 32
  • 43
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • 1
    This explains why the DateTImeFormat *timeZoneName: 'short'* option sometimes shows the offset and other times shows the abbreviation. Thanks. :-) – RobG May 26 '20 at 20:34
  • Yeah, it's a quirk of CLDR data that some names are assigned at the country level rather than the language level. Not sure why, but so it is. – Matt Johnson-Pint May 26 '20 at 20:40
  • 1
    @MattJohnson-Pint `DateTime` has `offsetNameLong` and `offsetNameShort` as instance properties, which are slightly clearer than the format token – snickersnack Jun 01 '20 at 02:36
  • updated Link: https://moment.github.io/luxon/#/formatting?id=table-of-tokens – Andy Gauge Oct 01 '21 at 22:17
-1

just simply use

const shortDate = new Date().toLocaleTimeString('en-us',{timeZoneName:'short'});
console.log(shotrDate.split(' ')[2]); // EST
Viktor Hardubej
  • 390
  • 7
  • 16
  • This version has several caveats like not working for AEST: https://stackoverflow.com/a/34405528/1101109 – pzrq Nov 28 '22 at 22:39