Sub topic 2
Daylight Saving Time and Other Calendar Pitfalls
This chapter assumes you know how a Date stores a millisecond time value, how local and UTC getters differ, and how to copy a date before changing it. It covers daylight saving time (DST), moving dates between systems, and turning an instant into readable text for any locale and time zone.
Daylight Saving Time and Other Calendar Pitfalls
A local day is not always 24 hours long. On a DST change, the day lasts 23 or 25 hours. If you add a fixed number of milliseconds, you move by elapsed time. If you call setDate, you move by calendar days.
// Run with: TZ=America/New_York node dst.js
const start = new Date(2024, 2, 9, 12, 0); // March 9, 2024, 12:00 local
const plus24h = new Date(start.getTime() + 24 * 60 * 60 * 1000);
const nextDay = new Date(start.getTime());
nextDay.setDate(nextDay.getDate() + 1);
console.log(plus24h.getHours()); // 13: clocks sprang forward on March 10
console.log(nextDay.getHours()); // 12: same wall-clock time, next dayChoose the method that matches what you mean. "Remind me in 24 hours" means adding milliseconds. "Same time tomorrow" means setDate. Month overflow is a related trap: January 31 plus one month becomes March 2 or March 3, depending on whether it is a leap year.
Storing and Sending Dates
Send dates as ISO 8601 UTC strings or as epoch milliseconds. JSON.stringify calls toISOString for you. JSON.parse does not turn the string back into a Date, so you must do that yourself.
const payload = JSON.stringify({ createdAt: new Date(Date.UTC(2024, 6, 4, 16, 30)) });
console.log(payload); // {"createdAt":"2024-07-04T16:30:00.000Z"}
const parsed = JSON.parse(payload);
console.log(typeof parsed.createdAt); // "string"
const createdAt = new Date(parsed.createdAt);
console.log(createdAt.getTime()); // 1720110600000Intl Basics and Intl.DateTimeFormat
Every Intl constructor takes two arguments. The first is a BCP 47 locale tag, such as "fr-CA", or a list of tags in order of preference. The second is an options object. resolvedOptions() shows which locale and settings the engine actually used.
const fmt = new Intl.DateTimeFormat(["fr-CA", "en"], { dateStyle: "long", timeZone: "UTC" });
console.log(fmt.format(new Date(Date.UTC(2024, 6, 4)))); // "4 juillet 2024"
console.log(fmt.resolvedOptions().locale); // "fr-CA"You can describe the output with dateStyle and timeStyle, or with component options such as year, month, and hour. You cannot combine the two approaches. Doing so throws a TypeError.
Displaying an Instant in Any Time Zone
The timeZone option takes an IANA name such as "Asia/Tokyo". It only changes how the instant is displayed. The stored value stays the same.
const instant = new Date("2024-07-04T16:30:00Z");
for (const timeZone of ["UTC", "Asia/Tokyo", "America/Los_Angeles"]) {
console.log(timeZone, instant.toLocaleString("en-GB", { timeZone, timeZoneName: "short" }));
}
// UTC shows 16:30 on 4 July, Tokyo 01:30 on 5 July, Los Angeles 09:30 on 4 JulyExercise
A server sends {"due":"2024-11-03T12:00:00.000Z"}. After JSON.parse, why does data.due.getTime() throw, and how do you fix it?
Show answer
JSON.parse leaves due as a string, and strings have no getTime method. Convert it first with new Date(data.due), then call getTime() or format it.