Javascript 使用偏移值获取时区缩写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28374901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get timezone abbreviation using offset value
提问by thetallweeks
Using moment.js (with moment-timezone), I want to get the timezone abbreviation (e.g. PST) for the current locale.
使用moment.js(带有moment-timezone),我想获取当前语言环境的时区缩写(例如PST)。
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".
我如何获得时区缩写?我在文档和其他地方看到的所有示例都选择了一个特定的区域,例如"America/New_York".
From the docs, it looks like I can get the information out of the Zone Objectwith zone.abbr(timestamp)but I'm not sure how to access the zone object.
从文档中,看起来我可以从Zone 对象中获取信息,zone.abbr(timestamp)但我不确定如何访问 zone 对象。
回答by Matt Johnson-Pint
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.
一般的问题是,没有完全可靠的方法来检测当前时区。这在这个答案中讨论。因此,moment-timezone 无法确定性地判断默认情况下应加载哪个时区。
There are some other options available though.
不过还有一些其他选项可用。
In some browsers, the ECMAScript Internationalization APIextensions are supported on the
toLocaleStringfunction of theDateobject. When supported, you can do this (without moment):var d = new Date(); // or whatever date you have var tzName = d.toLocaleString('en', {timeZoneName:'short'}).split(' ').pop();In supported 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.
You could use a script like jsTimeZoneDetectto guess at the local time zone. It's usuallycorrect, 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')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')
在某些浏览器中,对象的功能支持ECMAScript 国际化 API扩展。支持时,您可以执行此操作(无需时刻):
toLocaleStringDatevar d = new Date(); // or whatever date you have var tzName = d.toLocaleString('en', {timeZoneName:'short'}).split(' ').pop();在支持的浏览器中,您将获得类似“EST”的值。不过,您可能想要进行某种测试,因为它不适用于所有浏览器。
您可以使用像jsTimeZoneDetect这样的脚本来猜测本地时区。它通常是正确的,但不能保证。然后您可以将该值传递给时刻时区。
var tzName = jstz.determine().name(); var m = moment(); var abbr = m.tz(tzName).zoneAbbr(); // or .format('z')现在还有对moment-timezone 中时区检测/猜测的内置支持:
var tzName = moment.tz.guess(); var abbr = m.tz(tzName).zoneAbbr(); // or .format('z')

