Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

countries-and-timezones

manuelmhtr1.4mMIT3.8.0TypeScript support: included

Minimalistic library to work with countries and timezones data.

Countries, Timezones, Country, Timezone, Time, Date, DST, IANA, ICANN

readme

countries-and-timezones

GitHub Actions Status NPM Downloads License: MIT

Minimalistic library to work with countries and timezones data. Updated with the IANA timezones database.

Usage

NodeJS

Install with npm or yarn:

npm install --save countries-and-timezones

Browser

Add the following script to your project (only ~9kb):

<!-- Latest version -->
<script
  src="https://cdn.jsdelivr.net/gh/manuelmhtr/countries-and-timezones@latest/dist/index.min.js"
  type="text/javascript"
></script>

<!-- Or specify a version -->
<script
  src="https://cdn.jsdelivr.net/gh/manuelmhtr/countries-and-timezones@v3.8.0/dist/index.min.js"
  type="text/javascript"
></script>

<!-- This will export a variable named "ct": -->
<script type="text/javascript">
  var data = ct.getCountry("MX");
  console.log(data);
</script>

API

.getCountry(id, options = {})

Returns a country referenced by its id.

Accepts a parameter with options.

Example

const ct = require("countries-and-timezones");

const country = ct.getCountry("DE");
console.log(country);

/*
Prints:

{
  id: 'DE',
  name: 'Germany',
  timezones: [ 'Europe/Berlin', 'Europe/Zurich' ]
}

*/

.getTimezone(name)

Returns a timezone referenced by its name.

Example

const ct = require("countries-and-timezones");

const timezone = ct.getTimezone("America/Los_Angeles");
console.log(timezone);

/*
Prints:

{
  name: 'America/Los_Angeles',
  countries: [ 'US' ],
  utcOffset: -480,
  utcOffsetStr: '-08:00',
  dstOffset: -420,
  dstOffsetStr: '-07:00',
  aliasOf: null
}

*/

.getAllCountries(options = {})

Returns an object with the data of all countries.

Accepts a parameter with options.

Example

const ct = require("countries-and-timezones");

const countries = ct.getAllCountries();
console.log(countries);

/*
Prints:

{
  AD: {
    id: 'AD',
    name: 'Andorra',
    timezones: [ 'Europe/Andorra' ]
  },
  AE: {
    id: 'AE',
    name: 'United Arab Emirates',
    timezones: [ 'Asia/Dubai' ]
  },
  AF: {
    id: 'AF',
    name: 'Afghanistan',
    timezones: [ 'Asia/Kabul' ]
  },
  AG: {
    id: 'AG',
    name: 'Antigua and Barbuda',
    timezones: [ 'America/Puerto_Rico' ]
  },
  ...
}

*/

.getAllTimezones(options = {})

Returns an object with the data of all timezones.

Accepts a parameter with options.

Example

const ct = require("countries-and-timezones");

const timezones = ct.getAllTimezones();
console.log(timezones);

/*
Prints:

{
  "Africa/Abidjan": {
    "name": "Africa/Abidjan",
    "countries": [
      "CI", "BF", "GH",
      "GM", "GN", "ML",
      "MR", "SH", "SL",
      "SN", "TG"
    ],
    "utcOffset": 0,
    "utcOffsetStr": "+00:00",
    "dstOffset": 0,
    "dstOffsetStr": "+00:00",
    "aliasOf": null
  },
  "Africa/Algiers": {
    "name": "Africa/Algiers",
    "countries": [
      "DZ"
    ],
    "utcOffset": 60,
    "utcOffsetStr": "+01:00",
    "dstOffset": 60,
    "dstOffsetStr": "+01:00",
    "aliasOf": null
  },
  "Africa/Bissau": {
    "name": "Africa/Bissau",
    "countries": [
      "GW"
    ],
    "utcOffset": 0,
    "utcOffsetStr": "+00:00",
    "dstOffset": 0,
    "dstOffsetStr": "+00:00",
    "aliasOf": null
  },
  ...
}

*/

.getTimezonesForCountry(id, options = {})

Returns an array with all the timezones of a country given its id.

Accepts a parameter with options.

Example

const ct = require("countries-and-timezones");

const timezones = ct.getTimezonesForCountry("MX");
console.log(timezones);

/*
Prints:

[
  {
    "name": "America/Bahia_Banderas",
    "countries": [ "MX" ],
    "utcOffset": -360,
    "utcOffsetStr": "-06:00",
    "dstOffset": -300,
    "dstOffsetStr": "-05:00",
    "aliasOf": null
  },
  {
    "name": "America/Cancun",
    "countries": [ "MX" ],
    "utcOffset": -300,
    "utcOffsetStr": "-05:00",
    "dstOffset": -300,
    "dstOffsetStr": "-05:00",
    "aliasOf": null
  },
  {
    "name": "America/Chihuahua",
    "countries": [ "MX" ],
    "utcOffset": -420,
    "utcOffsetStr": "-07:00",
    "dstOffset": -360,
    "dstOffsetStr": "-06:00",
    "aliasOf": null
  },
  ...
}

*/

.getCountriesForTimezone(name, options = {})

Returns a list of the countries that uses a timezone given its name. When a timezone has multiple countries the first element is more relevant due to its geographical location.

Accepts a parameter with options.

Example

const ct = require("countries-and-timezones");

const timezone = ct.getCountriesForTimezone("Europe/Zurich");
console.log(timezone);

/*
Prints:

[
  {
    "id": "CH",
    "name": "Switzerland",
    "timezones": [
      "Europe/Zurich"
    ]
  },
  {
    "id": "DE",
    "name": "Germany",
    "timezones": [
      "Europe/Berlin",
      "Europe/Zurich"
    ]
  },
  {
    "id": "LI",
    "name": "Liechtenstein",
    "timezones": [
      "Europe/Zurich"
    ]
  }
]

*/

.getCountryForTimezone(name, options = {})

Returns a the most relevant country (due to its geographical location) that uses a timezone given its name.

Accepts a parameter with options.

Example

const ct = require("countries-and-timezones");

const timezone = ct.getCountryForTimezone("Europe/Zurich");
console.log(timezone);

/*
Prints:

{
  "id": "CH",
  "name": "Switzerland",
  "timezones": [
    "Europe/Zurich"
  ]
}

*/

options

Available options for functions are:

Parameter Type Description
deprecated Boolean Indicates if the result should include deprecated timezones or not. By default no deprecated timezones are included.

Data models

Country

A country is defined by the following parameters:

Parameter Type Description
id String The country ISO 3166-1 code.
name String Preferred name of the country.
timezones Array[String] The list of timezones used in the country.
{
  id: 'MX',
  name: 'Mexico',
  timezones: [
    'America/Bahia_Banderas',
    'America/Cancun',
    'America/Chihuahua',
    'America/Hermosillo',
    'America/Matamoros',
    'America/Mazatlan',
    'America/Merida',
    'America/Mexico_City',
    'America/Monterrey',
    'America/Ojinaga',
    'America/Tijuana'
  ]
}

Timezone

A timezone is defined by the following parameters:

Parameter Type Description
name String The name of the timezone, from tz database.
countries [String] A list of ISO 3166-1 codes of the countries where it's used. Etc/*, GMT and UTC timezones don't have associated countries.
utcOffset Number The difference in minutes between the timezone and UTC.
utcOffsetStr String The difference in hours and minutes between the timezone and UTC, expressed as string with format: ±[hh]:[mm].
dstOffset Number The difference in minutes between the timezone and UTC during daylight saving time (DST). When utcOffset and dstOffset are the same, means that the timezone does not observe a daylight saving time.
dstOffsetStr String The difference in hours and minutes between the timezone and UTC during daylight saving time (DST, expressed as string with format: ±[hh]:[mm].
aliasOf String The name of a primary timezone in case this is an alias. null means this is a primary timezone.
deprecated Boolean true when the timezone has been deprecated, otherwise this property is not returned.
{
  name: 'Asia/Tel_Aviv',
  countries: [ 'IL' ],
  utcOffset: 120,
  utcOffsetStr: '+02:00',
  dstOffset: 180,
  dstOffsetStr: '+03:00',
  aliasOf: 'Asia/Jerusalem',
  deprecated: true
}

Support

Consider sponsoring this project.

Working on something more complex?

Meet Spott:

  • Search any city, country or administrative division in the world. By full strings or autocompletion.
  • Find a place by an IP address.
  • Access to more than 240,000 geographical places. In more than 20 languages.

Spott API for cities, countries and administrative divisions

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file.

[3.8.0] - 2025-04-12

Changed

  • Updated with 2025b IANA Time Zone Database.

[3.7.2] - 2024-10-14

Fixed

  • Add script to add missing ESM package.json

[3.7.1] - 2024-10-13

Changed

  • Set up linting (by JC Franco)
  • Remove build output from source control (by JC Franco)
  • Add releasing script (by JC Franco)
  • Update test action to run on node versions 8 - 20 (by JC Franco)
  • Set up conventional commits PR check (by JC Franco)
  • Fix dependabot config (by JC Franco)

[3.7.0] - 2024-09-30

Changed

  • Updated with 2024b IANA Timezones database.

[3.6.0] - 2023-12-10

Changed

  • Updated with 2023c IANA Timezones database.

[3.5.2] - 2023-11-05

Fixed

  • #58 Missing export for CommonJS.
  • #60 Fixed utcOffsetStr calculation for negative offsets non-multiple of 60.

[3.5.1] - 2023-07-02

Changed

  • Updated with 2023a IANA Timezones database.

Added

  • TS Types export (#57 by Haz).

Removed

  • Countries: BV (Bouvet Island) and HM (Heard Island and McDonald Islands) since their are dependent territories, nobody lives there and they don't have official timezones.

[3.4.1] - 2023-01-29

Fixed

  • Broken shield on Readme.

[3.4.0] - 2022-12-04

Changed

Fixed

  • Wrong utcOffset for some timezones #51.

[3.3.0] - 2021-10-18

Added

  • Added new parameter options to most functions.
  • Added deprecated option to return both current and deprecated timezones.

Changed

  • Updated with 2021c IANA Timezones database.
  • Deprecated timezones are not returned by default (deprecated option is required).

[3.2.3] - 2021-09-28

Fixed

  • Constant declarations on Types (#41)

[3.2.1] - 2021-08-17

Fixed

Added

  • Eslint config

[3.2.0] - 2021-08-10

Added

[3.1.0] - 2021-08-04

Added

  • Restores getCountryForTimezone method to keep compatibility with version 2.
  • Adds esm path to package.json files (by Simon VDB)

[3.0.0] - 2021-07-28

Added

  • #32 Added support for ESM named exports

Changed

  • Timezones might now be related with multiple countries.

Removed

  • Removed deprecated timezones: America/Fort_Wayne, America/Godthab, America/Indianapolis, Asia/Rangoon, Singapore, US/East-Indiana, W-SU.

Breaking changes

  • getCountryForTimezone method was renamed to getCountriesForTimezone since now timezones might be related with multiple countries. The first country in the list is more relevant due to its geographical location.
  • The timezone property country was renamed to countries and is a list of strings rather than a single string.

[2.5.0] - 2021-07-18

Added

[2.4.0] - 2021-04-24

Changed

  • Updated with 2021a IANA Timezones database.

Removed

  • US/Pacific-New timezone.

[2.3.0] - 2020-08-25

Added

  • Support for <script/> tag.

Changed

  • Added 1 new timezone ("America/Nuuk").
  • Updated with 2020a IANA Timezones database.

[2.2.0] - 2020-04-18

Added

  • Updated 18 existing timezones.
  • Added 89 new timezones.
  • Complying with 2019c IANA Timezones database.

[2.1.0] - 2020-03-31

Added

[2.0.0] - 2019-10-10

Added

  • getTimezone method.
  • getCountry method.
  • 167 new alias timezones.
  • Property dstOffset in timezones.
  • Property dstOffsetStr in timezones.

Changed

  • Added all timezone aliases (before only primary timezones where supported). This increasead the support from 338 timezones to 505.
  • Minified data.json as possible. Reduced size 72%, from 88.1 KB to 24.9 KB.
  • Replaced countries (array) timezones property for country (string). Now all alias timezones are included, then each timezone has only one country assigned.
  • Renamed offsetStr to utcOffsetStr, to be more specific whether the offset is for UTC or DST.
  • getCountriesForTimezone method was renamed to getCountryForTimezone since now timezones have only 1 country. Only return a country object, instead of a countries array.

Removed

  • .raw property. Instead use getAllCountries and getAllTimezones methods.