Sub topic 10
Time-Zone-Aware Values with ZonedDateTime
This chapter assumes you already know Temporal.Instant, the Plain types, and IANA time zone identifiers from the earlier chapters. You should also be comfortable running ES module code in Node.js or a browser, either with native Temporal or with the polyfill.
Temporal.ZonedDateTime is the most complete Temporal type. It stores one exact moment, the time zone that moment is viewed in, and a calendar. From these three pieces it can work out every wall-clock field, the current UTC offset, and how long the local day is. Use it when you need both the exact time and the local time, for example when scheduling a meeting, a recurring reminder, or anything else that has to follow local daylight saving rules.
What a ZonedDateTime Contains
Internally, a ZonedDateTime holds an epoch nanosecond count (the same value an Instant holds), a time zone ID, and a calendar ID. It does not store fields like hour or offset. It computes them from the time zone rules each time you read them.
This design means the offset can never fall out of sync with the time zone. A ZonedDateTime cannot claim -05:00 for a New York moment in July, because the offset comes from the rules and is never stored separately.
Creating ZonedDateTime Values
The most common way to create one is from an ISO 8601 string with a bracketed time zone annotation, or from a property bag that includes timeZone.
const meeting = Temporal.ZonedDateTime.from('2024-03-15T09:00[America/New_York]');
console.log(meeting.toString());
// '2024-03-15T09:00:00-04:00[America/New_York]'
const london = Temporal.ZonedDateTime.from({
timeZone: 'Europe/London',
year: 2024,
month: 7,
day: 1,
hour: 9,
});
console.log(london.toString());
// '2024-07-01T09:00:00+01:00[Europe/London]'
// The constructor takes epoch nanoseconds as a BigInt
const epoch = new Temporal.ZonedDateTime(0n, 'UTC');
console.log(epoch.toString());
// '1970-01-01T00:00:00+00:00[UTC]'
// The current moment in the system time zone
console.log(Temporal.Now.zonedDateTimeISO().timeZoneId);You can also attach a zone to existing values. instant.toZonedDateTimeISO(tz) keeps the exact moment. plainDateTime.toZonedDateTime(tz) keeps the wall-clock time and works out which instant it refers to. The conversions chapter covers both directions in detail.
Reading Fields and Zone Information
A ZonedDateTime exposes all the PlainDateTime fields plus several zone-specific properties.
const zdt = Temporal.ZonedDateTime.from('2024-03-15T09:00[America/New_York]');
console.log(zdt.year, zdt.month, zdt.day, zdt.hour); // 2024 3 15 9
console.log(zdt.timeZoneId); // 'America/New_York'
console.log(zdt.offset); // '-04:00'
console.log(zdt.offsetNanoseconds); // -14400000000000
console.log(zdt.epochMilliseconds); // 1710507600000
console.log(zdt.hoursInDay); // 24
const springForward = Temporal.ZonedDateTime.from('2024-03-10T12:00[America/New_York]');
console.log(springForward.hoursInDay); // 23
console.log(springForward.startOfDay().toString());
// '2024-03-10T00:00:00-05:00[America/New_York]'hoursInDay and startOfDay() are worth knowing about. On transition days a day is not 24 hours long, and in some zones midnight does not exist at all. In those zones startOfDay() returns the first valid moment of the day, so you never have to assume 00:00.
Changing the Zone vs Changing the Fields
Two operations look alike but do opposite things:
| Operation | Keeps | Changes |
|---|---|---|
| `withTimeZone(tz)` | The exact instant | Wall-clock fields and offset |
| `toPlainDateTime().toZonedDateTime(tz)` | Wall-clock fields | The exact instant |
const ny = Temporal.ZonedDateTime.from('2024-03-15T09:00[America/New_York]');
// Same moment, seen from London (still on GMT in mid-March)
console.log(ny.withTimeZone('Europe/London').toString());
// '2024-03-15T13:00:00+00:00[Europe/London]'
// Same clock reading, different moment
console.log(ny.toPlainDateTime().toZonedDateTime('Europe/London').toString());
// '2024-03-15T09:00:00+00:00[Europe/London]'Use withTimeZone when you want to answer "what time is this call for my colleague?" Use the PlainDateTime path when you want "9 a.m. local, wherever I am."
Offset Conflicts When Parsing
A string can contain both an offset and a bracketed zone. If they disagree, for example because the zone's rules changed after the string was stored, from throws by default. The offset option lets you choose how to resolve the conflict.
const stale = '2024-03-15T09:00-05:00[America/New_York]'; // real offset is -04:00
try {
Temporal.ZonedDateTime.from(stale);
} catch (e) {
console.log(e instanceof RangeError); // true
}
// Trust the wall-clock time and the zone
console.log(Temporal.ZonedDateTime.from(stale, { offset: 'ignore' }).toString());
// '2024-03-15T09:00:00-04:00[America/New_York]'
// Trust the exact moment the offset describes
console.log(Temporal.ZonedDateTime.from(stale, { offset: 'use' }).toString());
// '2024-03-15T10:00:00-04:00[America/New_York]'The fourth option, 'prefer', uses the offset when it is valid for the zone and otherwise falls back to the wall-clock time. Choose based on what your stored data means. For future events that users entered as local times, 'ignore' usually matches their intent. For log timestamps, 'use' keeps the moment that was recorded.
Common Errors
- Leaving out the zone annotation. As shown above,
fromrequires the bracketed zone. If you want an exact time without a zone, useInstant. - Treating equals as "same moment".
equalsalso compares the time zone and calendar. The same instant in New York and London is notequals, butTemporal.ZonedDateTime.comparereturns0for the pair because it compares only the exact time. - Using ZonedDateTime for zone-free data. Birthdays and store opening hours have no meaningful instant. Forcing them into a zone produces wrong results when the zone changes, so a Plain type is the better fit.
- Storing only the offset. If you save
-04:00instead ofAmerica/New_York, you lose the rules that later arithmetic depends on. Store the full string fromtoString().
Exercise
What do Temporal.ZonedDateTime.from('2024-11-03T12:00[America/New_York]').hoursInDay and the offset of that value return, and why?
Show answer
hoursInDay is 25 and offset is '-05:00'. On 3 November 2024, New York clocks fell back from 02:00 EDT to 01:00 EST, which repeats one hour. By noon the zone is on standard time, UTC-5.
Exercise
You store '2025-06-01T18:00-04:00[America/New_York]' for a user's evening event. The zone's rules later change so that New York is at -05:00 on that date. Which offset option keeps the event at 6 p.m. local time, and what would the default behavior do?
Show answer
Use { offset: 'ignore' } (or 'prefer', which falls back to the wall-clock time when the offset is no longer valid). Both keep 18:00 local time with the new offset. The default, 'reject', throws a RangeError because the stored offset no longer matches the zone's rules.