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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

That 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