Install countries-and-timezones via npm
devTo use this library in a NodeJS project, install it using npm or yarn.
npm install --save countries-and-timezonesrepository·dev·Indexed 42 days ago
https://github.com/manuelmhtr/countries-and-timezonesA minimalistic library for working with country and timezone data, updated with the IANA timezones database. Version 3.10.0 provides utilities to retrieve country objects by ISO 3166-1 IDs, timezone objects by IANA names, and mappings between countries and their respective timezones.
To use this library in a NodeJS project, install it using npm or yarn.
npm install --save countries-and-timezonesYou can include the library in your project via a <script> tag using a CDN. The library exports a global variable named ct.
<!-- 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>
<script type="text/javascript">
var data = ct.getCountry("MX");
console.log(data);
</script>Most API functions accept an options object. The following parameter is currently available:
| Parameter | Type | Description |
|---|---|---|
deprecated | Boolean | Indicates if the result should include deprecated timezones or not. By default, no deprecated timezones are included. |
Returns an object containing the data for all countries, keyed by their country ID.
const ct = require("countries-and-timezones");
const countries = ct.getAllCountries();
console.log(countries);
/*
Prints:
{
AD: {
id: 'AD',
name: 'Andorra',
timezones: [ 'Europe/Andorra' ]
},
...
}
*/Returns a country object referenced by its ISO 3166-1 id.
const ct = require("countries-and-timezones");
const country = ct.getCountry("DE");
console.log(country);
/*
Prints:
{
id: 'DE',
name: 'Germany',
timezones: [ 'Europe/Berlin', 'Europe/Zurich' ]
}
*/Returns the single most relevant country object (based on geographical location) that uses the provided timezone name.
const ct = require("countries-and-timezones");
const timezone = ct.getCountryForTimezone("Europe/Zurich");
console.log(timezone);
/*
Prints:
{
"id": "CH",
"name": "Switzerland",
"timezones": [ "Europe/Zurich" ]
}
*/Returns an object containing the data for all timezones, keyed by their timezone name.
const ct = require("countries-and-timezones");
const timezones = ct.getAllTimezones();
console.log(timezones);
/*
Prints:
{
"Africa/Abidjan": {
"name": "Africa/Abidjan",
"countries": [ "CI", "BF", ... ],
"utcOffset": 0,
"utcOffsetStr": "+00:00",
"dstOffset": 0,
"dstOffsetStr": "+00:00",
"aliasOf": null
},
...
}
*/Returns a timezone object referenced by its name from the IANA/tz database.
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
}
*/Returns an array of all timezone objects used within a specific country, identified by its id.
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
},
...
]
*/Returns a list of country objects that use a specific timezone name. Note: When a timezone is shared by multiple countries, the first element in the returned array is considered more relevant due to its geographical location.
const ct = require("countries-and-timezones");
const timezone = ct.getCountriesForTimezone("Europe/Zurich");
console.log(timezone);
/*
Prints:
[
{
"id": "CH",
"name": "Switzerland",
"timezones": [ "Europe/Zurich" ]
},
...
]
*/The project uses xo for linting. Configuration is defined in xo.config.ts using the FlatXoConfig type. You can specify files to ignore and override ESLint/XO rules.
Key configuration options include:
ignores: An array of file patterns to skip during linting.prettier: Boolean to enable Prettier integration.space: Boolean to enable space-based formatting.rules: An object containing specific rule overrides for @typescript-eslint, import-x, n, and unicorn plugins.import type { FlatXoConfig } from "xo";
const xoConfig: FlatXoConfig = [
{
ignores: ["vite.config.ts"],
},
{
prettier: true,
space: true,
rules: {
"@typescript-eslint/ban-types": "off",
"import-x/extensions": ["error", "never"],
"unicorn/prevent-abbreviations": [
"error",
{
allowList: {
dst: true,
},
},
],
},
},
];
export default xoConfig;A country object contains the following fields:
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',
...
]
}