javascript 显示用户本地区域缩写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24174807/
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
Display users local zone abbreviation
提问by HypeXR
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
.
我试图使用z
或zz
格式来显示字符串末尾的时区,但发现它已被弃用。Z
并ZZ
努力显示-0700
,但我希望它显示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:
我得到moment-timezone
了数据文件,但我不知道如何使用它来确定客户端的本地时区并显示如下内容:
2014-05-30T22:56:23.967 PDT
2014-05-30T22:56:23.967 PDT
instead of:
代替:
2014-05-30T22:56:23.967
2014-05-30T22:56:23.967
Is it possible to use moment-timezone
to determine EST, CST, MST, PST, etc?
是否可以用于moment-timezone
确定 EST、CST、MST、PST 等?
回答by Matt Johnson-Pint
As of moment-timezone 0.1.0, you can use the formatting additionsto get the abbreviation of a specifictime zone:
从 moment-timezone 0.1.0 开始,您可以使用格式添加来获取特定时区的缩写:
moment().tz("America/Los_Angeles").format('z'); // PST or PDT
As of version 0.5.0, you can now guess the user's localtime zone, which when combined with the above technique, allows you to do the following:
从 0.5.0 版本开始,您现在可以猜测用户的本地时区,结合上述技术,您可以执行以下操作:
moment().tz(moment.tz.guess()).format('z');
Assuming the guess was correct, it will display the associated abbreviation.
假设猜测是正确的,它将显示相关的缩写。
回答by user3459287
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(')'));
回答by crash springfield
2020 Update
2020 更新
Some time in the past 5 years, this stopped working. Now try:
在过去 5 年的某个时候,这停止了工作。现在尝试:
var timeZone = moment.tz.guess();
var time = new Date();
var timeZoneOffset = time.getTimezoneOffset();
moment.tz.zone(timeZone).abbr(timeZoneOffset);
回答by jjbskir
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