Get and set offset by time zone name
This module pairs nicely with date-fns
.
npm install @kidonng/tz-offset
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
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.
declare const withTz: (timeZone: string, date: number | string | Date) => Date
Converts the given date to specified timezone.
This module uses the following features:
This module is a simplifed version of date-fns-tz
's tzTokenizeDate
and tzParseTimezone
internals.