What is timezone-support?
The timezone-support npm package provides utilities for working with time zones, including converting dates between time zones, parsing and formatting dates, and handling daylight saving time transitions.
What are timezone-support's main functionalities?
Convert Date Between Time Zones
This feature allows you to convert a date from one time zone to another. The code sample demonstrates converting a UTC date to 'America/New_York' time zone.
const { convertTimeToTimeZone } = require('timezone-support');
const { parseFromTimeZone, formatToTimeZone } = require('date-fns-timezone');
const date = new Date('2023-10-01T12:00:00Z');
const timeZone = 'America/New_York';
const convertedDate = convertTimeToTimeZone(date, { timeZone });
console.log(convertedDate);
Parse Date from Time Zone
This feature allows you to parse a date string in a specific time zone. The code sample demonstrates parsing a date string in the 'America/New_York' time zone.
const { parseFromTimeZone } = require('timezone-support');
const dateStr = '2023-10-01 12:00:00';
const timeZone = 'America/New_York';
const parsedDate = parseFromTimeZone(dateStr, { timeZone });
console.log(parsedDate);
Format Date to Time Zone
This feature allows you to format a date to a specific time zone. The code sample demonstrates formatting a UTC date to 'America/New_York' time zone with a specific format.
const { formatToTimeZone } = require('timezone-support');
const date = new Date('2023-10-01T12:00:00Z');
const timeZone = 'America/New_York';
const format = 'YYYY-MM-DD HH:mm:ss [GMT]Z (z)';
const formattedDate = formatToTimeZone(date, { timeZone, format });
console.log(formattedDate);
Other packages similar to timezone-support
moment-timezone
Moment-timezone is an add-on for Moment.js that provides time zone support. It allows you to parse, manipulate, and display dates in different time zones. Compared to timezone-support, moment-timezone is more widely used and has a larger community, but it requires Moment.js as a dependency.
luxon
Luxon is a modern JavaScript library for working with dates and times. It provides comprehensive time zone support and is designed to be a replacement for Moment.js. Luxon is more modern and has a smaller footprint compared to moment-timezone, but it offers similar functionalities to timezone-support.
date-fns-tz
Date-fns-tz is a time zone extension for date-fns, a modern JavaScript date utility library. It provides functions for working with time zones, including converting, parsing, and formatting dates. Date-fns-tz is modular and tree-shakeable, making it a lightweight alternative to timezone-support.
Time Zone Support
Lightweight time zone listing and date converting. Intended for adding time zone support to high-level date libraries, but also for direct application usage.
- Tiny code base - 4.6 KB minified, 1.7 KB gzipped. Do not pack unnecessary weight in your application.
- Packed time zone data - 924 KB minified, 33.6 KB gzipped. Single time zones are unpacked on demand.
- Smaller bundles of code with limited data - 1900-2050 (206 kB minified, 25.4 kB gzipped), 1970-2038 (141 kB minified, 15.8 kB gzipped) and 2012-2022 (31.3 KB minified, 8.25 kB gzipped).
- Generated from the official time zone database version 2022f. Canonical time zone names, aliases, UTC offsets, and daylight-saving time changes.
- ESM, UMD and CJS module formats provided.
- Minimal interface for time zone lookup and conversions. Parsing, formatting and manipulating dates is usually the task for a higher-level date library.
Attention: exported identifiers in vanilla browser modules changed in the version 2.0.0. See the migration guide for more information.
Table of Contents
Synopsis
const {
listTimeZones, findTimeZone, getZonedTime, getUnixTime
} = require('timezone-support')
const timeZones = listTimeZones()
const berlin = findTimeZone('Europe/Berlin')
const nativeDate = new Date()
const berlinTime = getZonedTime(nativeDate, berlin)
const berlinTime = { year: 2018, month: 9, day: 2, hours: 10, minutes: 0 }
const nativeDate = new Date(getUnixTime(berlinTime, berlin))
Installation and Getting Started
This module can be installed in your project using NPM, PNPM or Yarn. Make sure, that you use Node.js version 14.8 or newer.
$ npm i timezone-support
$ pnpm i timezone-support
$ yarn add timezone-support
Functions are exposed as named exports from the package modules, for example:
const { findTimeZone, getZonedTime } = require('timezone-support')
You can read more about the module loading in other environments, like with ES6 or in web browsers. Usage scenarios demonstrate applications of this library in typical real-world scenarios. Design concepts explain the approach to time zone handling taken by tni library and types of values used ion the interface. Generating custom time zone data will allow you to save the overall package size by limiting the supported year span. Finally, the API reference lists all functions with a description of their functionality.
You can see complete sample applications too, which can help you start with integration of this library.
Library Integrations
- Day.js - timeZone plugin supplies parsing from and formatting to an arbitrary time zone
- date-fns - date-fns-timezone provides functions for parsing from and formatting to an arbitrary time zone and time zone conversions for the native
Date
object.
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
License
Copyright (c) 2018-2022 Ferdinand Prantl
Licensed under the MIT license.