Sub topic 16
Daylight Saving Time and Ambiguous Wall-Clock Times
This chapter assumes you can create a Temporal.ZonedDateTime, turn a PlainDateTime into one, and do basic arithmetic with add. The examples use the global Temporal object. If your runtime lacks it, load the polyfill as shown in the runtime support chapter.
Gaps and Overlaps
Most of the time, one wall-clock time in a time zone maps to exactly one instant. Daylight saving time (DST) breaks that rule twice a year.
- Gap (spring forward): clocks jump ahead, so some local times never happen. In
America/New_Yorkon 2024-03-10, the clock goes from 01:59:59 straight to 03:00. The time 02:30 does not exist that day. - Overlap (fall back): clocks move back, so some local times happen twice. On 2024-11-03, New York shows 01:30 first at offset
-04:00(EDT) and then again an hour later at-05:00(EST).
PlainDateTime has no time zone, so it has no gaps or overlaps. The problem appears only when you attach a wall-clock time to a zone. Temporal makes you control how that step resolves through the disambiguation option.
The disambiguation Option
| Value | Gap (time skipped) | Overlap (time repeated) |
|---|---|---|
| `'compatible'` (default) | Shift forward by the gap length | Earlier of the two instants |
| `'earlier'` | Shift backward by the gap length | Earlier of the two instants |
| `'later'` | Shift forward by the gap length | Later of the two instants |
| `'reject'` | Throw `RangeError` | Throw `RangeError` |
'compatible' is the default because it matches what legacy Date and most other libraries do. That makes migrated code behave the same way.
Resolving a gap
const zone = 'America/New_York';
const missing = Temporal.PlainDateTime.from('2024-03-10T02:30');
for (const disambiguation of ['compatible', 'earlier', 'later']) {
const zdt = missing.toZonedDateTime(zone, { disambiguation });
console.log(disambiguation.padEnd(10), zdt.toString());
}
// compatible 2024-03-10T03:30:00-04:00[America/New_York]
// earlier 2024-03-10T01:30:00-05:00[America/New_York]
// later 2024-03-10T03:30:00-04:00[America/New_York]
try {
missing.toZonedDateTime(zone, { disambiguation: 'reject' });
} catch (e) {
console.log(e instanceof RangeError); // true
}For a gap, 'earlier' and 'later' do not pick a "nearest valid time" such as 01:59 or 03:00. They move the wall-clock time by the length of the gap, which is one hour here.
Resolving an overlap
const zone = 'America/New_York';
const repeated = Temporal.PlainDateTime.from('2024-11-03T01:30');
const first = repeated.toZonedDateTime(zone, { disambiguation: 'earlier' });
const second = repeated.toZonedDateTime(zone, { disambiguation: 'later' });
console.log(first.toString()); // '2024-11-03T01:30:00-04:00[America/New_York]'
console.log(second.toString()); // '2024-11-03T01:30:00-05:00[America/New_York]'
console.log(first.until(second).toString()); // 'PT1H'Both results show 01:30 on the clock, but they are one real hour apart.
Offsets in Strings: the offset Option
A string such as 2024-11-03T01:30:00-05:00[America/New_York] carries both a UTC offset and a zone name. The offset settles the overlap: -05:00 can only mean the second 01:30. This is why ZonedDateTime.prototype.toString() always writes the offset, and why storing that string round-trips exactly.
The offset and the zone can disagree. This usually happens when a value was saved before a government changed the zone's rules. The offset option decides what wins:
| Value | Behavior when the offset is invalid for the zone |
|---|---|
| `'reject'` | Throw `RangeError` (default for `from`) |
| `'use'` | Trust the offset: keep the exact instant, recompute wall-clock time |
| `'ignore'` | Trust the wall-clock time: drop the offset, then apply `disambiguation` |
| `'prefer'` | Use the offset if valid, otherwise act like `'ignore'` (default for `with`) |
// New York is at -04:00 in July, so -05:00 is wrong for this zone
const stale = '2024-07-01T12:00-05:00[America/New_York]';
try {
Temporal.ZonedDateTime.from(stale);
} catch (e) {
console.log(e instanceof RangeError); // true
}
console.log(Temporal.ZonedDateTime.from(stale, { offset: 'use' }).toString());
// '2024-07-01T13:00:00-04:00[America/New_York]'
console.log(Temporal.ZonedDateTime.from(stale, { offset: 'ignore' }).toString());
// '2024-07-01T12:00:00-04:00[America/New_York]'
console.log(Temporal.ZonedDateTime.from(stale, { offset: 'prefer' }).toString());
// '2024-07-01T12:00:00-04:00[America/New_York]'Choose 'use' when the instant matters more, such as a log timestamp. Choose 'ignore' or 'prefer' when the local time matters more, such as "the meeting is at noon."
Why with() defaults to 'prefer'
When you change one field with with(), Temporal tries to keep the current offset. This stops an edit from jumping to the other side of an overlap:
const second130 = Temporal.ZonedDateTime.from('2024-11-03T01:30-05:00[America/New_York]');
console.log(second130.with({ minute: 45 }).toString());
// '2024-11-03T01:45:00-05:00[America/New_York]' (stays in EST)If with() used plain 'compatible' resolution, the result would be the earlier 01:45 at -04:00. That is a time before the value you started with.
Arithmetic Across a Transition
Calendar units (days, weeks, months) change the wall-clock time and then resolve it again. If the result lands in a gap, disambiguation applies. Exact units (hours and smaller) add real elapsed time and never land in a gap.
const before = Temporal.ZonedDateTime.from('2024-03-09T02:30[America/New_York]');
console.log(before.add({ days: 1 }).toString());
// '2024-03-10T03:30:00-04:00[America/New_York]' (02:30 does not exist)
console.log(before.add({ hours: 24 }).toString());
// '2024-03-10T03:30:00-04:00[America/New_York]' (same result, different reason)These two results match only because the starting time sits inside the gap window. At noon they would differ, as the arithmetic chapter showed.
Inspecting Transitions
Two properties let you detect DST days without hard-coding dates:
const zone = 'America/New_York';
const jan1 = Temporal.ZonedDateTime.from({ year: 2024, month: 1, day: 1, timeZone: zone });
const next = jan1.getTimeZoneTransition('next');
console.log(next.toString()); // '2024-03-10T03:00:00-04:00[America/New_York]'
console.log(next.hoursInDay); // 23
console.log(Temporal.ZonedDateTime.from('2024-11-03T12:00[America/New_York]').hoursInDay); // 25getTimeZoneTransition accepts 'next' or 'previous'. It returns null when the zone has no further transitions, which is the case for UTC. hoursInDay is useful when you build hourly charts, because a DST day has 23 or 25 slots instead of 24.
Common Mistakes
- Treating
{ days: 1 }and{ hours: 24 }as the same thing. Use days for "same time tomorrow" and hours for "24 hours from now." - Storing a zoned value as a
PlainDateTimeplus a zone name and no offset. Overlap information is lost. Store the fulltoString()output. - Relying on the default
'compatible'for user input when an hour shift would be a bug.
Exercise
A user schedules an alarm for 01:30 on 2024-11-03 in America/New_York and expects it to ring the second time 01:30 occurs. Which call creates the right ZonedDateTime, and what offset will it show?
Show answer
Use Temporal.PlainDateTime.from('2024-11-03T01:30').toZonedDateTime('America/NewYork', { disambiguation: 'later' }). The result is '2024-11-03T01:30:00-05:00[America/NewYork]', the second occurrence, in EST.
Exercise
A database holds '2024-07-01T09:00-05:00[America/New_York]' for a recurring 9 a.m. stand-up, but New York uses -04:00 in July. Which offset option keeps the meeting at 9 a.m. local time, and what does ZonedDateTime.from return with no option?
Show answer
Use { offset: 'ignore' } or { offset: 'prefer' }. Both give '2024-07-01T09:00:00-04:00[America/New_York]'. With no option, from uses offset: 'reject' and throws a RangeError.
Exercise
Write an expression that returns true when a given ZonedDateTime zdt falls on a day that contains a DST transition.
Show answer
zdt.hoursInDay !== 24. Spring-forward days have 23 hours and fall-back days have 25. Some zones use 30-minute shifts, which give 23.5 or 24.5, and the check catches those as well.