Replacements for timezone-support
timezone-support ships its own copy of the IANA time zone database and helpers to convert between zones. Modern runtimes already ship this data: Intl.supportedValuesOf lists the available zones, and Temporal covers the conversion helpers.
Intl.supportedValuesOf
Intl.supportedValuesOf replaces listTimeZones:
import { listTimeZones } from 'timezone-support'
const timeZones = listTimeZones()
const timeZones = Intl.supportedValuesOf('timeZone') Temporal
findTimeZone is no longer needed — pass the IANA identifier directly.
Getting the wall-clock time in a zone (getZonedTime, convertDateToTime):
import { findTimeZone, getZonedTime } from 'timezone-support'
const date = new Date()
const berlin = findTimeZone('Europe/Berlin')
const time = getZonedTime(date, berlin)
const time = date.toTemporalInstant().toZonedDateTimeISO('Europe/Berlin') Getting the UTC offset (getUTCOffset):
import { findTimeZone, getUTCOffset } from 'timezone-support'
const berlin = findTimeZone('Europe/Berlin')
const { offset } = getUTCOffset(new Date(), berlin)
const { offset } = Temporal.Now.zonedDateTimeISO('Europe/Berlin') Note that Temporal returns the offset as a string ('+02:00') or via offsetNanoseconds, rather than minutes.
Getting a timestamp or Date from a zoned time (getUnixTime, convertTimeToDate):
import { findTimeZone, getUnixTime } from 'timezone-support'
const berlin = findTimeZone('Europe/Berlin')
const time = { year: 2018, month: 9, day: 2, hours: 10, minutes: 0 }
const epoch = getUnixTime(time, berlin)
const epoch = Temporal.ZonedDateTime.from({
timeZone: 'Europe/Berlin',
year: 2018, month: 9, day: 2, hour: 10, minute: 0
}).epochMilliseconds
const date = new Date(epoch)Attaching a zone to a wall-clock time (setTimeZone):
import { findTimeZone, setTimeZone } from 'timezone-support'
const berlin = findTimeZone('Europe/Berlin')
const time = { year: 2018, month: 9, day: 2, hours: 10, minutes: 0 }
const berlinTime = setTimeZone(time, berlin)
const berlinTime = Temporal.PlainDateTime
.from({ year: 2018, month: 9, day: 2, hour: 10, minute: 0 })
.toZonedDateTime('Europe/Berlin') Parsing and formatting (parseZonedTime, formatZonedTime) — Temporal parses ISO 8601 strings and toLocaleString (backed by Intl.DateTimeFormat) handles output; custom format strings such as 'D.M.YYYY H:mm zZ' are not supported:
import { parseZonedTime, formatZonedTime } from 'timezone-support/parse-format'
const time = parseZonedTime('2.9.2018 10:00 CEST+02:00', 'D.M.YYYY H:mm zZ')
const time = Temporal.ZonedDateTime.from('2018-09-02T10:00:00+02:00[Europe/Berlin]')
const output = formatZonedTime(time, 'D.M.YYYY H:mm zZ')
const output = time.toLocaleString('de-DE', { dateStyle: 'medium', timeStyle: 'short' })