/tz-offset

Get and set offset by time zone name

Primary LanguageTypeScriptMIT LicenseMIT

tz-offset

Get and set offset by time zone name

This module pairs nicely with date-fns.

Install

npm install @kidonng/tz-offset

Usage

import {tzOffset, withTz} from '@kidonng/tz-offset'

tzOffset('Asia/Shanghai') // => 480
tzOffset('Etc/GMT+8') // => -480 (the sign is intentionally inverted, see https://en.wikipedia.org/wiki/Tz_database#Area)

withTz('Asia/Shanghai', Date.UTC(2000, 0)) // => 2000-01-01T08:00:00.000Z

API

tzOffset

declare const tzOffset: (timeZone: string) => number

Returns number of minutes between the given time zone and UTC.

The value is positive if the given time zone is ahead of UTC, and negative if the given time zone is behind UTC.

Note: This is contrary to Date#getTimezoneOffset(), which compares local time zone to UTC.

withTz

declare const withTz: (timeZone: string, date: number | string | Date) => Date

Converts the given date to specified timezone.

Compatibility

This module uses the following features:

Credits

This module is a simplifed version of date-fns-tz's tzTokenizeDate and tzParseTimezone internals.