3
votes

I want to use Moment's guess() function to return the timezone continent (or country) and city as in their example:

moment.tz.guess(); // America/Chicago

But the above doesn't work. The returned value is CET, which is something else. It's an abbreviation of the Central European Time, i.e. no continent-country, no city.

How do I use the guess() function to return 'Europe/Stockholm', 'America/Chicago' etc?

4

4 Answers

11
votes

This happens when the host environment (browser, etc.) implements the API for ECMA-402 but returns the wrong type of time zone in the result. If you are seeing this with Moment, you can probably also get this result without Moment with the following:

Intl.DateTimeFormat().resolvedOptions().timeZone

Presently, Moment assumes that if this API exists, that it either provides a valid IANA time zone identifier or returns undefined. However, some environments return things like CET as you pointed out.

Ignoring bad results is already recorded as a feature request in moment/moment-timezone#423. In that particular case, the environment was an older Chrome browser on an Android device.

0
votes

According to momentjs library documentation, you can do this:

moment.tz([2012, 0], 'America/New_York').zoneAbbr(); // would return "EST"

Here's the reference: https://momentjs.com/timezone/docs/#/using-timezones/formatting/

0
votes

I know I'm very late to this, but I was searching for an answer to the same problem. For anyone else looking for the same answer, using the following should work:

moment.tz.guess()

For reference, I'm using both moment-with-locales.js and moment-timezone-with-data.js, and just using the simple moment.tz.guess() will return the full timezone name for me, ie: America/Denver or America/New_York, etc.

-1
votes

Try using moment.format('Z'), you will get -05:00 or something. And then map it to CET or whatever you are using based on predefined map.