Skip to content Skip to sidebar Skip to footer

Display Users Local Zone Abbreviation

I was trying to use z or zz in format to display the timezone at the end of the string and found that it's deprecated. Z and ZZ work to display -0700, but instead I want it to show

Solution 1:

As of moment-timezone 0.1.0, you can use the formatting additions to get the abbreviation of a specific time zone:

moment().tz("America/Los_Angeles").format('z');  // PST or PDT

As of version 0.5.0, you can now guess the user's local time zone, which when combined with the above technique, allows you to do the following:

moment().tz(moment.tz.guess()).format('z');

Assuming the guess was correct, it will display the associated abbreviation.

Solution 2:

From the latest moment timezone docs

moment.tz(String).format("Z z"); // -08:00 PST
moment.tz(String).zoneAbbr();    // PST
moment.tz(String).zoneName();    // PST

Some examples:

moment.tz(moment.tz.guess()).zoneAbbr(); // PST
moment.tz("America/New_York").zoneAbbr(); // EST

Solution 3:

2020 Update

Some time in the past 5 years, this stopped working. Now try:

var timeZone = moment.tz.guess();
var time = newDate();
var timeZoneOffset = time.getTimezoneOffset();
moment.tz.zone(timeZone).abbr(timeZoneOffset);

Solution 4:

After searching a lot, this seems to work.

const t = moment.tz.guess();
timeZone = moment.tz(t).zoneAbbr();;

Solution 5:

Probably this is not a good way, but I did not want to add any additional scripts in my project for local time zone, so I investigated a moment object and I get the local time zone by the code I wrote below:

 var test = momentUpdateDate._d.toString().substring(test.indexOf('(')+1, test.indexOf(')'));

Post a Comment for "Display Users Local Zone Abbreviation"