Search code examples
javascriptdatetime-formatintl

Get timezone names using en-US locale


While using the Intl.DateTimeFormat, I noticed that the timezone short names are not set when using a locale which does not have the specified timezone, for example en-US locale does not contain the Australia/Sydney timezone information.

Is there a way to display AEST while keeping the en-US locale? Otherwise we'll have to map IANA timezones to locales, which seems overkill.

var date = new Date();
var options = {
  hour: 'numeric', minute: 'numeric', second: 'numeric', 
  timeZone: 'Australia/Sydney',
  timeZoneName: 'short'
};

// displays 12:14:27 am AEST
console.log(new Intl.DateTimeFormat('en-AU', options).format(date));

// 12:14:27 AM GMT+10
console.log(new Intl.DateTimeFormat('en-US', options).format(date));


Solution

  • To my knowledge you're out of luck without writing your own potentially risky code to handle what you're looking for.

    Intl (and most JS frameworks that handle l10n and i18n) usually piggyback off of the CLDR. The CLDR defines rules for things like this. In this case for en-US there is no "specific short" version of the name so it falls back to the localized GMT version.

    I recommend you just leave it alone and live with it. Any kind of per-locale hackery is not a good idea since you'd have to potentially do that to multiple locales, then deal with those changes when Intl gets updated as the CLDR is constantly being evaluated.