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

javascript - Get timezone abbreviation using offset value - Stack Overflow

programmeradmin0浏览0评论

Using moment.js (with moment-timezone), I want to get the timezone abbreviation (e.g. PST) for the current locale.

var now = Date.now(); // 1423254073931
var zone = moment(now).zone(); // 480
var timezone = 

How do I get the timezone abbreviation? All of the examples I have seen in the docs and elsewhere pick a specific region like "America/New_York".

From the docs, it looks like I can get the information out of the Zone Object with zone.abbr(timestamp) but I'm not sure how to access the zone object.

JSFiddle

Using moment.js (with moment-timezone), I want to get the timezone abbreviation (e.g. PST) for the current locale.

var now = Date.now(); // 1423254073931
var zone = moment(now).zone(); // 480
var timezone = 

How do I get the timezone abbreviation? All of the examples I have seen in the docs and elsewhere pick a specific region like "America/New_York".

From the docs, it looks like I can get the information out of the Zone Object with zone.abbr(timestamp) but I'm not sure how to access the zone object.

JSFiddle

Share Improve this question edited Apr 21, 2015 at 22:53 Matt Johnson-Pint 242k75 gold badges462 silver badges607 bronze badges asked Feb 6, 2015 at 21:13 thetallweeksthetallweeks 7,4056 gold badges27 silver badges25 bronze badges 6
  • And you can't use /\(([^)]+)\)/.exec(new Date())[1] ? – Rob Raisch Commented Feb 6, 2015 at 21:45
  • @RobRaisch I don't think that will work cross browser. For instance, this is the result of new Date in Firefox: 2015-02-06T23:40:57.085Z – thetallweeks Commented Feb 6, 2015 at 23:42
  • Actually, the result of new Date() is a new instance of a Date object which FF appears to print to the console in a non-standard format. So instead of the above, you can /\(([^)]+)\)/.exec((new Date()).toString())[1] which here in Boston, returns "Eastern Standard Time" – Rob Raisch Commented Feb 6, 2015 at 23:52
  • My mistake, I see that with toString, the date looks the same in Chrome and Firefox. But even with this fix, I still have inconsistencies across browsers (some say EST while others say Eastern Standard Time). This is why I was hoping to utilize moment.js, since it normalizes many things across various browsers. – thetallweeks Commented Feb 7, 2015 at 1:44
  • Yeah, it's a nice idea, but the output of the date object in string format is completely implementation specific. The ES5 spec does not guarantee any specific results, and in practice you'll find browsers vary the results between versions and OS. – Matt Johnson-Pint Commented Feb 7, 2015 at 1:59
 |  Show 1 more comment

2 Answers 2

Reset to default 36

The title and the question are different. In the title, you ask how to get it using the offset - which would not be possible. There are many time zones that share the same offset, so it isn't possible to distinguish a time zone abbreviation from an offset alone.

But in the question, you asked how to get the abbreviation for the current locale, for a specific timestamp.

The general problem is, there is no fully-reliable way to detect the current time zone. This is discussed in this answer. So moment-timezone can't deterministically tell which time zone should be loaded by default.

There are some other options available though.

Current browsers / node

In current browsers, the ECMAScript Internationalization API extensions are supported on the toLocaleString function of the Date object. When supported, you can do this:

    var d = new Date(); // or whatever date you have
    var tzName = d.toLocaleString('en', {timeZoneName:'short'}).split(' ').pop();

In current browsers, you'll get a value like "EST". You might want to do some sort of tests though, because it won't work in all browsers.

Use jsTimeZoneDetect

You could use a script like jsTimeZoneDetect to guess at the local time zone. It's usually correct, but not guaranteed. You could then pass that value to moment-timezone.

    var tzName = jstz.determine().name();
    var m = moment();
    var abbr = m.tz(tzName).zoneAbbr();  // or .format('z')

Use moment-timezone

There is also now built-in support for time zone detection/guessing in moment-timezone:

    var tzName = moment.tz.guess();
    var abbr = m.tz(tzName).zoneAbbr();  // or .format('z')

Latest build of moment.js would give you timezone guess: PR#220 for example: moment.tz.guess(); will result in 'America/New_York'

发布评论

评论列表(0)

  1. 暂无评论