Sub topic 9
Time-Zone-Aware Values with ZonedDateTime
This chapter assumes you already know Temporal.Instant, the Plain types, and IANA time zone identifiers such as America/New_York. The examples use a global Temporal, either native or from the polyfill set up earlier.
What a ZonedDateTime Holds
Temporal.ZonedDateTime combines three things: an exact instant, a time zone, and a calendar. From these it can work out the local wall-clock time and the UTC offset in effect at that moment. Use it when a value has to stay correct as the offset changes, for example a meeting in New York or a flight departure from Paris.
Creating a ZonedDateTime
The string form puts the time zone in square brackets after the date and time. You can also pass a property bag that includes a timeZone key.
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 dinner = Temporal.ZonedDateTime.from({
timeZone: 'Europe/London',
year: 2024, month: 7, day: 1, hour: 18
});
console.log(dinner.toString());
// '2024-07-01T18:00:00+01:00[Europe/London]'Temporal filled in the offset in both cases. New York had already switched to daylight saving time on March 10, so the offset is -04:00. London uses +01:00 in summer.
Reading Its Properties
A ZonedDateTime exposes the wall-clock fields (year, hour, and so on), the exact-time fields, and some zone details.
const zdt = Temporal.ZonedDateTime.from('2024-03-10T12:00[America/New_York]');
console.log(zdt.timeZoneId); // 'America/New_York'
console.log(zdt.offset); // '-04:00'
console.log(zdt.hour); // 12
console.log(zdt.hoursInDay); // 23 (the spring-forward day)
console.log(zdt.epochMilliseconds); // 1710086400000hoursInDay shows that a calendar day in a time zone does not always last 24 hours.
Same Moment, Different Zone
withTimeZone keeps the instant and changes only the zone. The wall-clock time is recalculated for the new zone.
const ny = Temporal.ZonedDateTime.from('2024-03-15T09:00[America/New_York]');
console.log(ny.withTimeZone('Asia/Tokyo').toString());
// '2024-03-15T22:00:00+09:00[Asia/Tokyo]'Common Errors
| Mistake | Result |
|---|---|
| String without a `[Zone]` annotation, e.g. `'2024-03-15T09:00-04:00'` | `RangeError`: an offset alone does not identify a time zone |
| Offset that disagrees with the zone, e.g. `'2024-03-15T09:00-05:00[America/New_York]'` | `RangeError` by default, because the offset is not valid for that zone on that date |
| Storing a fixed offset instead of a zone name | Future DST changes are missed |
How Temporal handles wall-clock times that occur twice or not at all is covered in the chapter on daylight saving time.
Exercise
A ZonedDateTime is 2024-11-02T20:00[America/Los_Angeles]. What does withTimeZone('UTC') return, and does the instant change?
Show answer
It returns '2024-11-03T03:00:00+00:00[UTC]'. Los Angeles is still on PDT (-07:00) on November 2, so 20:00 local time is 03:00 UTC the next day. The instant stays the same. Only the time zone, and so the wall-clock reading, changes.
Exercise
Why does Temporal.ZonedDateTime.from('2024-03-15T09:00Z') throw?
Show answer
The string has no bracketed time zone annotation. A ZonedDateTime needs a time zone, and 'Z' only says the time is in UTC. Use '2024-03-15T09:00Z[UTC]', or parse the string with Temporal.Instant.from and then call toZonedDateTimeISO.