4.0.0 • Published 2 years ago

@kidonng/tz-offset v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

tz-offset

Get and set offset by time zone name

Install

npm install @kidonng/tz-offset

Usage

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.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. 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.

Recipes

This module pairs nicely with date-fns.

Specify a UTC offset

tzOffset('Etc/GMT+8') // => -480

Note 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.

4.0.0

2 years ago

3.0.0

2 years ago

1.2.0

2 years ago

2.0.0

2 years ago

1.1.0

3 years ago

1.0.0

3 years ago