Sub topic 6
Daylight Saving Time and Other Calendar Pitfalls
This chapter assumes you know the difference between local and UTC getters and setters, and that you copy a Date before you change it. Both were covered in earlier chapters.
Most date bugs come from treating two kinds of time as one. Elapsed time is a fixed number of milliseconds. Calendar time is "the same wall-clock time, one day later." The two usually match. They differ when a daylight saving time (DST) change falls in between.
A day is not always 24 hours
In a zone with DST, one local day each year has 23 hours and another has 25. The example below behaves this way only in such a zone, so run it with TZ=America/New_York node dst.js. In 2024, New York's clocks jumped forward on March 10.
const HOUR = 60 * 60 * 1000;
const mar10 = new Date(2024, 2, 10); // local midnight, March 10
const mar11 = new Date(2024, 2, 11); // local midnight, March 11
console.log((mar11 - mar10) / HOUR); // 23
// Adding 24 hours of elapsed time misses midnight
const plus24h = new Date(mar10.getTime() + 24 * HOUR);
console.log(plus24h.getDate(), plus24h.getHours()); // 11 1
// Adding one calendar day keeps the wall-clock time
const nextDay = new Date(mar10.getTime());
nextDay.setDate(nextDay.getDate() + 1);
console.log(nextDay.getDate(), nextDay.getHours()); // 11 0Use millisecond arithmetic for durations such as timeouts and session lengths. Use the local setters for calendar steps such as "tomorrow at 9 AM."
Skipped and repeated local times
On the spring-forward day, 02:00 through 02:59 never happens. If you ask for 02:30, the engine moves the time forward instead of failing:
const gap = new Date(2024, 2, 10, 2, 30); // TZ=America/New_York
console.log(gap.getHours(), gap.getMinutes()); // 3 30In autumn, 01:00 through 01:59 happens twice. For an ambiguous time like this, the specification picks the earlier of the two instants. If a user really meant the second 01:30, a bare Date cannot express that. You need an explicit offset, such as "2024-11-03T01:30-05:00".
Counting calendar days
Dividing a local difference by 86,400,000 gives 0.958... across a spring-forward change. To count calendar days, convert each date's year, month, and day to a UTC value, then subtract. UTC has no DST:
function calendarDaysBetween(a, b) {
const ua = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const ub = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return (ub - ua) / 86400000;
}
console.log(calendarDaysBetween(new Date(2024, 2, 9), new Date(2024, 2, 11))); // 2Month ends and leap years
setMonth rolls overflow into the next month, so January 31 plus one month becomes March 2. Leap years cause the same problem: February 29, 2024 plus one year becomes March 1, 2025. Most billing and scheduling code wants the result clamped to the last valid day instead:
function addMonthsClamped(date, months) {
const result = new Date(date.getTime());
const day = result.getDate();
result.setDate(1); // avoid overflow while changing the month
result.setMonth(result.getMonth() + months);
const lastDay = new Date(result.getFullYear(), result.getMonth() + 1, 0).getDate();
result.setDate(Math.min(day, lastDay));
return result;
}
console.log(addMonthsClamped(new Date(2024, 0, 31), 1).getDate()); // 29 (Feb 2024)
console.log(addMonthsClamped(new Date(2024, 1, 29), 12).getMonth()); // 1 (still February)Day 0 of a month means the last day of the month before it, which is why new Date(year, month + 1, 0) gives the length of month.
Exercise
A reminder app stores the next reminder as last.getTime() + 7 * 24 * 60 * 60 * 1000. Users in Berlin say that after late March their weekly 9:00 reminder fires at 10:00. Why, and what is the fix?
Show answer
The code adds a fixed 168 hours. The week that contains the spring-forward change has only 167 local hours, so the reminder lands one hour later on the wall clock. Copy the date and call next.setDate(next.getDate() + 7) so the local time stays at 9:00.
Exercise
What does addMonthsClamped(new Date(2023, 4, 31), 1) return, and what would a plain setMonth(5) give?
Show answer
The clamped version returns June 30, 2023. A plain setMonth(5) asks for June 31, which overflows to July 1.