admin管理员组文章数量:1287647
I would like to retrieve a list of timezones with utc offsets from momentjs by country name. For example, I would like to have something like
momentObject.getTimezones("America")
Which would return a list of timezones like
['UTC−11:00 (ST) — American Samoa, Jarvis Island, Kingman Reef, Midway Atoll, Palmyra Atoll',
....
Does functionality like this exist in the library?
I would like to retrieve a list of timezones with utc offsets from momentjs by country name. For example, I would like to have something like
momentObject.getTimezones("America")
Which would return a list of timezones like
['UTC−11:00 (ST) — American Samoa, Jarvis Island, Kingman Reef, Midway Atoll, Palmyra Atoll',
....
Does functionality like this exist in the library?
Share Improve this question edited Aug 19, 2017 at 1:03 Daniel 3,5143 gold badges36 silver badges47 bronze badges asked Aug 19, 2017 at 0:47 sakurashinkensakurashinken 4,0788 gold badges39 silver badges73 bronze badges3 Answers
Reset to default 4That functionality has been requested, and is pending in the following pull request:
https://github./moment/moment-timezone/pull/410
However, it would accept a country code (such as US
), and would return a list of time zone identifiers, such as America/Los_Angeles
. Note that the list you gave are not the values that would be returned.
Also keep in mind that one cannot assign a single offset to an entire time zone without considering point-in-time, so that would not be a part of the functionality either. See "time zone != offset" in the timezone tag wiki.
moment-timezone
MomentJS offers a moment-timezone package:
moment.tz.names();
would return an Array of all Timezones. These Timezones are prefixed with their respective continents; America/Detroit
. You could easily filter this Array by Continent using plain JavaScript.
countries-and-timezones
To get a country/timezone matching you would definitely need another package like countries-and-timezones which offers a simple API to get Timezones by ISO country code:
countriesAndTimezones.getTimezonesForCountry('MX');
returns:
[ { name: 'America/Mexico_City',
utcOffset: -360,
offsetStr: '-06:00',
countries: [ 'MX' ] },
{ name: 'America/Cancun',
utcOffset: -300,
offsetStr: '-05:00',
countries: [ 'MX' ] },
{ name: 'America/Merida',
utcOffset: -360,
offsetStr: '-06:00',
countries: [ 'MX' ] },
{ name: 'America/Monterrey',
...
Note, that there is an n x m relation between Countries and Timezones (hence a Timezone may live in multiple countries). Therefore this package also contains a getCountriesForTimezone()
method.
Anyone looking on how to get timezone by country code in momentjs without waiting for https://github./moment/moment-timezone/pull/410 to be merged, you can thankfully get the .json file from moment package and retrieve the country object containing timezones.
Take a look: https://github./moment/moment-timezone/blob/develop/data/meta/latest.json And this ment should be helpful too: https://github./moment/moment-timezone/issues/289#issuement-354585784
本文标签: javascriptHow to find available timezones in a country using momentjsStack Overflow
版权声明:本文标题:javascript - How to find available timezones in a country using momentjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741224942a2361682.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论