brijesh-pant
brijesh-pant

Reputation: 1145

Get long offset name for a timezone using moment-timezone

The luxon library adds this feature to get the offsetNameLong acc. to timezone ianaName which provides a locale specific translated zone name. For instance

DateTime.local().setLocale("en-US").setZone("America/Los_Angeles").offsetNameLong // prints Pacific Daylight Time
DateTime.local().setLocale("ja-JP").setZone("America/Los_Angeles").offsetNameLong // prints アメリカ太平洋夏時間

Because of luxon less IE browser support(unless using polyfills), I'd like to do something similar using moment-timezone. I'm just curious if moment-timezone provides a similar feature to do the same, or, any other alternative to achieve this functionality.

Upvotes: 2

Views: 650

Answers (1)

Abhilash
Abhilash

Reputation: 2256

Looks like moment.js doesn't support long names of timezones, however offers a custom way to use long names instead of abbreviations that zoneAbbr() returns.

Reference: https://momentjs.com/timezone/docs/#/using-timezones/formatting/

var abbrs = {
    EST : 'Eastern Standard Time',
    EDT : 'Eastern Daylight Time',
    CST : 'Central Standard Time',
    CDT : 'Central Daylight Time',
    MST : 'Mountain Standard Time',
    MDT : 'Mountain Daylight Time',
    PST : 'Pacific Standard Time',
    PDT : 'Pacific Daylight Time',
};

moment.fn.zoneName = function () {
    var abbr = this.zoneAbbr();
    return abbrs[abbr] || abbr;
};

moment.tz([2012, 0], 'America/New_York').format('zz');    // Eastern Standard Time
moment.tz([2012, 5], 'America/New_York').format('zz');    // Eastern Daylight Time
moment.tz([2012, 0], 'America/Los_Angeles').format('zz'); // Pacific Standard Time
moment.tz([2012, 5], 'America/Los_Angeles').format('zz'); // Pacific Daylight Time

Upvotes: 2

Related Questions