4.0.0 • Published 2 years ago
@kidonng/tz-offset v4.0.0
tz-offset
Get and set offset by time zone name
Install
npm install @kidonng/tz-offsetUsage
import {tzOffset, withTz} from '@kidonng/tz-offset'
tzOffset('Asia/Shanghai') // => 480
tzOffset('UTC') // => 0
withTz(Date.UTC(2000, 0), 'Asia/Shanghai') // => 2000-01-01T08:00:00.000ZAPI
tzOffset
declare const tzOffset: (timeZone: string) => numberReturns 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. This is contrary to Date#getTimezoneOffset(), which compares local time zone to UTC.
withTz
declare const withTz: (timeZone: string, date: number | string | Date) => DateConverts the given date to specified timezone.
Recipes
This module pairs nicely with date-fns.
Specify a UTC offset
tzOffset('Etc/GMT+8') // => -480Note the sign is intentionally inverted.
Compatibility
This modules uses Intl.DateTimeFormat's hourCycle, which is supported since:
- Chrome 73
- Edge 18
- Firefox 58
- Safari 13
- Node.js 12
- Deno 1.8
Credits
This module is a simplifed version of date-fns-tz's tzTokenizeDate and tzParseTimezone internals.