Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

countries-and-timezones

Package Overview
Dependencies
0
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    countries-and-timezones

Minimalistic library to work with countries and timezones data.


Version published
Weekly downloads
202K
increased by43.27%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

countries-and-timezones

A minimalistic library to work with countries and timezones data.

Install

npm install --save countries-and-timezones

Data models

Country

A country is defined by the following parameters:

ParameterTypeDescription
idStringThe country ISO 3166-1 code.
nameStringPreferred name of the country.
timezonesArray[String]The list of timezones used in the country.
{
  id: 'MX',
  name: 'Mexico',
  timezones: [
    'America/Bahia_Banderas',
    'America/Cancun',
    'America/Chihuahua',
    'America/Ensenada',
    'America/Hermosillo',
    'America/Matamoros',
    'America/Mazatlan',
    'America/Merida',
    'America/Mexico_City',
    'America/Monterrey',
    'America/Ojinaga',
    'America/Santa_Isabel',
    'America/Tijuana'
  ]
}

Timezone

A timezone is defined by the following parameters:

ParameterTypeDescription
nameStringThe name of the timezone, from tz database.
countryStringThe ISO 3166-1 code of the country where it's used. Etc/*, GMT and UTC timezones don't have and associated country.
utcOffsetNumberThe difference in minutes between the timezone and UTC.
utcOffsetStrStringThe difference in hours and minutes between the timezone and UTC, expressed as string with format: ±[hh]:[mm].
dstOffsetNumberThe 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.
dstOffsetStrStringThe difference in hours and minutes between the timezone and UTC during daylight saving time (DST, expressed as string with format: ±[hh]:[mm].
aliasOfStringThe name of a primary timezone in case this is an alias. null means this is a primary timezone.
{
  name: 'Asia/Tel_Aviv',
  country: 'IL',
  utcOffset: 120,
  utcOffsetStr: '+02:00',
  dstOffset: 180,
  dstOffsetStr: '+03:00',
  aliasOf: 'Asia/Jerusalem'
}

API

.getCountry(id)

Returns a country referenced by its id.

Example

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

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

/*
Prints:

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

*/

.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',
  country: 'US',
  utcOffset: -480,
  utcOffsetStr: '-08:00',
  dstOffset: -420,
  dstOffsetStr: '-07:00',
  aliasOf: null
}

*/

.getAllCountries()

Returns an object with the data of all countries.

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/Antigua' ]
  },
  ...
}

*/

.getAllTimezones()

Returns an object with the data of all timezones.

Example

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

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

/*
Prints:

{
  'America/Los_Angeles': {
    name: 'America/Los_Angeles',
    country: 'US',
    utcOffset: -480,
    utcOffsetStr: '-08:00',
    dstOffset: -420,
    dstOffsetStr: '-07:00',
    aliasOf: null
  },
  'Africa/Abidjan': {
    name: 'Africa/Abidjan',
    country: 'CI',
    utcOffset: 0,
    utcOffsetStr: '+00:00',
    dstOffset: 0,
    dstOffsetStr: '+00:00',
    aliasOf: null
  },
  'Africa/Accra': {
    name: 'Africa/Accra',
    country: 'GH',
    utcOffset: 0,
    utcOffsetStr: '+00:00',
    dstOffset: 0,
    dstOffsetStr: '+00:00',
    aliasOf: null
  },
  ...
}

*/

.getTimezonesForCountry(id)

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

Example

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

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

/*
Prints:

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

*/

.getCountryForTimezone(name)

Returns the country that uses a timezone given its name.

Example

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

const timezone = ct.getCountryForTimezone('Asia/Tokyo');
console.log(timezone);

/*
Prints:

{
  id: 'JP',
  name: 'Japan',
  timezones: [ 'Asia/Tokyo' ]
}

*/

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

Keywords

FAQs

Last updated on 01 Apr 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc