Using Moment.js to display the users local zone ab

2019-01-26 05:11发布

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.

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:

2014-05-30T22:56:23.967 PDT

instead of:

2014-05-30T22:56:23.967

Is it possible to use moment-timezone to determine EST CST ... MST PST?

2条回答
小情绪 Triste *
2楼-- · 2019-01-26 05:38

As of moment-timezone 0.1.0, you can use the formatting additions to get the abbreviation of a specific time zone:

moment().tz("America/Los_Angeles").format('z');  // PST or PDT

As of version 0.5.0, you can now guess the user's local time zone, which when combined with the above technique, allows you to do the following:

moment().tz(moment.tz.guess()).format('z');

Assuming the guess was correct, it will display the associated abbreviation.

查看更多
Summer. ? 凉城
3楼-- · 2019-01-26 05:39

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(')'));
查看更多
登录 后发表回答