Security News
Namecheap Takes Down Polyfill.io Service Following Supply Chain Attack
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
countries-and-timezones
Advanced tools
Readme
A minimalistic library to work with countries and timezones data. Updated with the IANA timezones database.
Install with npm or yarn:
npm install --save countries-and-timezones
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.js" type="text/javascript"></script>
<!-- Or specify a version -->
<script src="https://cdn.jsdelivr.net/gh/manuelmhtr/countries-and-timezones@v3.0.1/dist/index.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>
.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/Busingen', 'Europe/Berlin' ]
}
*/
.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()
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:
{
'Africa/Bamako': {
name: 'Africa/Bamako',
countries: [ 'ML' ],
utcOffset: 0,
utcOffsetStr: '+00:00',
dstOffset: 0,
dstOffsetStr: '+00:00',
aliasOf: 'Africa/Abidjan'
},
'Africa/Banjul': {
name: 'Africa/Banjul',
countries: [ 'GM' ],
utcOffset: 0,
utcOffsetStr: '+00:00',
dstOffset: 0,
dstOffsetStr: '+00:00',
aliasOf: 'Africa/Abidjan'
},
'Africa/Conakry': {
name: 'Africa/Conakry',
countries: [ 'GN' ],
utcOffset: 0,
utcOffsetStr: '+00:00',
dstOffset: 0,
dstOffsetStr: '+00:00',
aliasOf: 'Africa/Abidjan'
},
...
}
*/
.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: 'Mexico/BajaSur',
countries: [ 'MX' ],
utcOffset: -420,
utcOffsetStr: '-07:00',
dstOffset: -360,
dstOffsetStr: '-06:00',
aliasOf: 'America/Mazatlan'
},
{
name: 'Mexico/General',
countries: [ 'MX' ],
utcOffset: -360,
utcOffsetStr: '-06:00',
dstOffset: -300,
dstOffsetStr: '-05:00',
aliasOf: 'America/Mexico_City'
},
{
name: 'America/Ensenada',
countries: [ 'MX' ],
utcOffset: -480,
utcOffsetStr: '-08:00',
dstOffset: -420,
dstOffsetStr: '-07:00',
aliasOf: 'America/Tijuana'
},
...
}
*/
.getCountriesForTimezone(name)
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.
Example
const ct = require('countries-and-timezones');
const timezone = ct.getCountriesForTimezone('Asia/Tokyo');
console.log(timezone);
/*
Prints:
[
{
"id": "CH",
"name": "Switzerland",
"timezones": [
"Europe/Zurich"
]
},
{
"id": "DE",
"name": "Germany",
"timezones": [
"Europe/Busingen",
"Europe/Berlin",
"Europe/Zurich"
]
},
{
"id": "LI",
"name": "Liechtenstein",
"timezones": [
"Europe/Vaduz",
"Europe/Zurich"
]
}
]
*/
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: [
'Mexico/BajaSur',
'Mexico/General',
'America/Ensenada',
'America/Santa_Isabel',
'Mexico/BajaNorte',
'America/Bahia_Banderas',
'America/Cancun',
'America/Chihuahua',
'America/Tijuana',
'America/Hermosillo',
'America/Matamoros',
'America/Mazatlan',
'America/Merida',
'America/Mexico_City',
'America/Monterrey',
'America/Ojinaga'
]
}
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. |
{
name: 'Asia/Tel_Aviv',
countries: [ 'IL' ],
utcOffset: 120,
utcOffsetStr: '+02:00',
dstOffset: 180,
dstOffsetStr: '+03:00',
aliasOf: 'Asia/Jerusalem'
}
This project is maintained by a single person, considering supporting by:
Meet Spott:
MIT
FAQs
Minimalistic library to work with countries and timezones data.
The npm package countries-and-timezones receives a total of 172,068 weekly downloads. As such, countries-and-timezones popularity was classified as popular.
We found that countries-and-timezones demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.