Display users local zone abbreviation
Asked Answered
G

6

22

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

I got moment-timezone and the data file, but I can't figure out how to use it to determine the client's local time zone and display something like:

2014-05-30T22:56:23.967 PDT

instead of:

2014-05-30T22:56:23.967

Is it possible to use moment-timezone to determine EST, CST, MST, PST, etc?

Globin answered 12/6, 2014 at 0:35 Comment(1)
My understanding of the (very minimal) documentation is that it's used for setting the timezone of an output string, not formatting the string, e.g. m.tz("America/Toronto").format(); // "2013-11-18T11:55:00-05:00". There are a number of utilities that attempt go guess a users timezone based on the timezone offset and whether daylight saving is observed. However they seem to work using the IANA timezone values, not "civil" (ambiguous) values like EST.Gam
A
46

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.

Autism answered 3/7, 2014 at 21:40 Comment(4)
'z' and 'zz' is depreciated now. how do we get the timezone in short formatToni
@Toni - You can still use them, but only with moment-timezone.Autism
how to get timezone using abbreviation. like " var mTimeZone = moment.tz("IST"); " insted of " var mTimeZone = moment.tz("Asia/Kolkata"); "P
@P - That would assume that IST only has one interpretation. In fact, Ireland, Israel, and India all use the abbreviation IST. Thus, such a function would be impossible to create.Autism
S
5

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
Swedenborgian answered 13/2, 2020 at 16:10 Comment(0)
L
2

2020 Update

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

var timeZone = moment.tz.guess();
var time = new Date();
var timeZoneOffset = time.getTimezoneOffset();
moment.tz.zone(timeZone).abbr(timeZoneOffset);
Listel answered 5/2, 2020 at 19:29 Comment(0)
T
2

After searching a lot, this seems to work.

const t = moment.tz.guess();
timeZone = moment.tz(t).zoneAbbr();;
Tyrannous answered 16/11, 2020 at 19:10 Comment(0)
I
0

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(')'));
Incunabulum answered 4/5, 2015 at 15:30 Comment(1)
You shouldn't use _d, but rather .toDate(), but still this isn't recommended as the output of toString is implementation specific and isn't guaranteed to be formatted in any particular way. In practice, the time zone data is sometimes abbreviated, sometimes expanded, sometimes absent.Autism
E
0

My post is not just for this question but let me record a thing here.

Somehow moment.tz.guess() does not work in my environment. Using pure javascript Intl instead gave me the correct timezone.

const intl = Intl.DateTimeFormat().resolvedOptions().timeZone // Asia/Tokyo
moment.tz(intl).zoneName() // JST
moment.tz(intl).zoneAbbr() // JST

fsfiddle https://jsfiddle.net/qoozy/9xajy84b/20/

Eugenioeugenius answered 25/2, 2023 at 1:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.