date-fns-tz
IANA timezone support for date-fns with formatting in a zone and conversions between zones.
Official Documentation ↗v3 (date-fns v4 compatible). Check the official docs for the latest API surface.Installation
npm install date-fns-tzpnpm add date-fns-tzMethods
No methods match this filter.
IANA timezone support for date-fns with formatting in a zone and conversions between zones.
Overview
date-fns-tz layers IANA timezones onto date-fns. formatInTimeZone renders an instant as wall time in any zone, toZonedTime and fromZonedTime convert between naive local fields and absolute instants, and getTimezoneOffset exposes DST-aware offsets at a date.
It complements date-fns formatting and arithmetic with zone awareness while keeping the functional API style. The core discipline stays: store instants, convert only for display.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
formatInTimeZone— Formats an absolute instant as wall-clock time in the named IANA timezone.toZonedTime— Returns a Date whose local fields read as the wall time of the instant in that zone.fromZonedTime— Interprets a wall-clock Date as time in the given zone and returns the UTC instant.getTimezoneOffset— Returns the offset in minutes for a zone at a specific instant, DST included.
Usage Notes
- Keep UTC instants in storage; convert only at the rendering edge.
- Prefer formatInTimeZone over toZonedTime plus format for one-shot display.
- Validate DST-ambiguous local times when business rules must reject them.
Common Pitfalls
- Persisting the output of toZonedTime stores a mutated local view, not the real instant.
- Server and browser local zones differ; never rely on the ambient zone in conversions.
- getHours after toZonedTime differs from toISOString, which still shows UTC.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/marnusw/date-fns-tz@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:49:04.454Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- An invalid date or time zone input will result in an Invalid Date passed to date-fns/format, which will throw a RangeError. (https://cdn.jsdelivr.net/gh/marnusw/date-fns-tz@latest/README.md)
- An invalid date string or time zone will result in an Invalid Date. (https://cdn.jsdelivr.net/gh/marnusw/date-fns-tz@latest/README.md)
- Similar to date-fns/format, when an invalid date is used a RangeError is thrown. (https://cdn.jsdelivr.net/gh/marnusw/date-fns-tz@latest/README.md)
FAQ
- Why does toZonedTime not change the instant?
- JavaScript Date is always an absolute instant. toZonedTime produces a Date whose local getters, getDate, getHours, read as the wall time in the target zone; the underlying timestamp is unchanged. Use it for display, never as storage.
- How do I store the result correctly?
- Store the original UTC instant, ISO strings or timestamps, and apply toZonedTime or formatInTimeZone only when rendering. Persisting zoned pseudo-Dates bakes a server or browser offset into data.
- What about DST boundary errors?
- Spring-forward gaps and fall-back overlaps have no unique mapping. fromZonedTime resolves them with ICU rules, shifted or repeated hour, which is usually right; if the business rule must reject ambiguous times, validate the boundary explicitly.
Report a problem with this API docOpens a pre-filled issue on GitHub.