最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return timezone name with Moment's guess() - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Oct 7, 2017 at 17:21 Fellow Stranger asked Oct 7, 2017 at 14:41 Fellow StrangerFellow Stranger 34k37 gold badges177 silver badges249 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 12

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.

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.

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/

In my case, I had this import on my code

import moment from "moment";

and I was getting the issue when I was using

property 'tz' does not exist on type 'typeof moment'.

moment.tz.guess();

I just replaced the import to use this

import momentTZ from "moment-timezone";

momentTZ.tz.guess();

And done!

Everything is working now.

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.

发布评论

评论列表(0)

  1. 暂无评论