Daylight Saving Time Edge Cases
You'll learn how Daylight Saving Time transitions create times that don't exist or exist twice, and why JavaScript's Date object handles them unpredictably.
The Problem with "Spring Forward" and "Fall Back"
Daylight Saving Time (DST) creates two bizarre scenarios twice a year:
Invalid times: When clocks "spring forward" (usually 2:00 AM → 3:00 AM), any time between 2:00 and 3:00 doesn't actually exist that day. If you try to create a Date for "2:30 AM" on that morning, JavaScript must guess what you meant.
Ambiguous times: When clocks "fall back" (3:00 AM → 2:00 AM), times between 2:00 and 3:00 occur twice. The time "2:30 AM" happens once before the shift and again after. Which one do you mean?
How Date Fails You
JavaScript's Date object handles these edge cases inconsistently:
// March 10, 2024: clocks spring forward at 2 AM (US)
const invalid = new Date(2024, 2, 10, 2, 30);
console.log(invalid.getHours()); // Often shows 3 (skipped ahead)
// November 3, 2024: clocks fall back at 2 AM
const ambiguous = new Date(2024, 10, 3, 2, 30);
// Which 2:30? First or second occurrence? Unpredictable!
Different browsers may interpret these differently. Date arithmetic near transitions can also produce unexpected jumps or duplicate hours.
Think of DST like a book with pages ripped out (spring) or duplicated (fall)—trying to reference those pages without context leads to confusion.
Key Takeaway: DST creates times that are invalid (don't exist) or ambiguous (exist twice), and JavaScript's Date object handles these scenarios inconsistently across browsers and operations, making DST-aware logic error-prone without specialized libraries.