A fast, lightweight, zero-dependency library to translate between Time Zones and UTC with native Intl.DateTimeFormat
in ~100 LoC. For Node.js & Browsers.

XTZ is a poor man’s Temporal polyfill, but just for time zones.
Demo: https://therootcompany.github.io/tz.js/
TZ.toLocalISOString(new Date());
TZ.timeZone();
TZ.toOffsetISOString("2021-11-07 03:15:59.000", "America/New_York");
TZ.toTimeZoneISOString("2021-03-14T07:15:59.000Z", "America/New_York");
Features
Compatible with Browsers, and Node.js.
Browsers
<script src="https://unpkg.com/xtz@latest/xtz.min.js"></script>
var TZ = window.XTZ;
Node.js & Webpack
npm install --save xtz
var TZ = require("xtz");
Demo
See https://therootcompany.github.io/tz.js/.
How was this built?
I live-streamed the creation of this entire project.
If you’d like to learn how I did it and what challenges I encountered, you can watch here:
https://www.youtube.com/playlist?list=PLxki0D-ilnqa6horOJ2G18WMZlJeQFlAt
(though there have been a few minor updates and bug fixes off-camera)
API
toLocalISOString(dateOrNull)
toTimeZone(utcDate, timeZone)
toTimeZoneISOString(isoString, timeZone)
fromTimeZone(dtString, timeZone)
toOffsetISOString(dtString, timeZone)
toTimeZone(utcDate, timeZone)
Convert UTC into a Target Time Zone
Use ISO timestamps representing the absolute UTC time (ISO with or without offset):
"2021-11-07T08:15:59.000Z"
var utcDate = TZ.toTimeZone("2021-03-14T07:15:59.000Z", "America/New_York");
utcDate.toISOString();
Convert directly to an ISO String:
TZ.toTimeZoneISOString("2021-11-07T08:15:59.000Z", "America/New_York");
Or use our bespoke (custom) date object:
var tzDate = TZ.toTimeZone("2021-11-07T08:15:59.000Z", "America/New_York");
You can also use a date object with an absolute ISO time:
var tzDate = TZ.toTimeZone(
new Date("2021-11-07T08:15:59.000Z"),
"America/New_York"
);
console.log(tzDate.toISOString());
Our ISO Strings + Offsets work with JavaScript’s native Date object!!
new Date("2021-11-07T03:15:59.000-0500").toISOString());
fromTimeZone(dtString, timeZone)
Convert a Target Time Zone into ISO
Use ISO-like timestamps representing the local time in the target time zone:
"2021-11-0 03:15:59.000"
var tzDate = TZ.fromTimeZone("2021-11-07 03:15:59.000", "America/New_York");
tzDate.toISOString();
Convert directly to an offset ISO String:
TZ.toOffsetISOString("2021-11-07 03:15:59.000", "America/New_York");
Or our bespoke date object:
var utcDate = TZ.fromTimeZone("2021-11-07 03:15:59.000", "America/New_York");
Use a Date as a source time
You can also use a date object as the source time, but the date’s UTC time will be treated as relative to time
zone rather than absolute (this is a workaround for JavaScript’s lack of bi-directional timezone support).
var utcDate = TZ.fromTimeZone(
new Date("2021-11-07T03:15:59.000Z"),
"America/New_York"
);
utcDate.toISOString();
Daylight Savings / Edge Cases
In 2021 Daylight Savings (in the US)
- begins at 2am on March 14th (skips to 3am)
- ends at 2am on November 7th (resets to 1am)
See https://www.timeanddate.com/time/change/usa.
Q: What happens in March when 2am is skipped?
Q: What happens in November when 1am happens twice?
List of Time Zones
See the Full List of Time Zones on Wikipedia.
Common Zones for Testing:
America/New_York -0500
America/Denver -0700
America/Phoenix -0700 (No DST)
America/Los_Angeles -0800
UTC Z
Australia/Adelaide +0930 (30-min, has DST)
Asia/Kathmandu +0545 (No DST, 45-min)
Asia/Kolkata +0530 (No DST, 30-min)
评论