How do I get timezones list using moment.js librar

2019-06-14 20:51发布

I need to make a <select> list of all actual world timezones. How do I get an array with timezones list?

标签: momentjs
6条回答
女痞
2楼-- · 2019-06-14 21:23

For timezone, you should manually add timezone JS file made by yourself.

In this page, you can pick the zones up and generate a file to be loaded after you load moment.js first off.

You should have something like this, take a look:

<script src="/javascripts/modules/moment/moment-with-langs.min.js"></script>
<script src="/javascripts/modules/moment/moment-timezone.min.js"></script>
<script src="/javascripts/modules/moment/moment-timezone-data.js"></script>

The JS moment-timezone-data.js is the custom list of the timezones.

However, so far I know, MomentJS does not return for you a list of all timezones, because timezone can be changed as well. If you need to populate a select tag, I suggest you parse the timezone list manually from the link above.

查看更多
Emotional °昔
3楼-- · 2019-06-14 21:33

First include moment-timezone with data and then you can use moment.tz.names();

查看更多
\"骚年 ilove
4楼-- · 2019-06-14 21:35

There are 378 timezones. A timezone is any combination of rules about time, including DST, that was in effect for any period of time, in any locale, no matter how tiny--such as an individual town in Indiana that decided to go off daylight savings time for two years in the 1940s. Or, to take a more recent example, the North Korean time zone, which was just moved up by 30 minutes. Or a dozen time zones just for Antarctica.

I doubt if you want to show all these timezones in your pulldown. You need to choose the most common 50-100 or so. You can either do that by yourself, or you can copy the list from some site like Google. You probably want to choose at least one timezone from each of the 24 offsets, so no matter where a person is, they can find one that sort of matches.

查看更多
贼婆χ
5楼-- · 2019-06-14 21:36

Include moment-timezone.js from cdn.

Now, use moment.tz._names to get the timezones as an object. The method moment.tz.names() also gives the timezones as an array, but its slower.

Versions prior to 0.4.0, don't have the moment.tz._names object hence moment.tz.names() method must be used.

查看更多
手持菜刀,她持情操
6楼-- · 2019-06-14 21:41

Beware that moment.js does not comes with moment-timezone.js by default, you have to get it.

bower install moment-timezone --save
npm install moment-timezone --save
Install-Package Moment.Timezone.js

Or Downlaod

Then you can require an array of timezones in this way:

var timezones = moment.tz.names();

Another useful trick is that you can set(guess) the current user timezone, so you don't have to make the user look for his timezone in a huge list, or you can set a timezone by default and then let the user change it if he needs it.

var currentUserTimezone = moment.tz.guess();

Also, you can get abbreviation of timezones if you need them, in case you have to provide and array of abbreviations of timezones:

moment.tz('America/Los_Angeles').format('z') //PDT

moment-timezone.js full documentation

查看更多
登录 后发表回答