Daylight Saving Time Edge Cases
You'll learn how DST transitions can throw off your date arithmetic and cause unexpected hour jumps or duplicates.
The Core Problem
Daylight Saving Time changes don't just affect clocks on your wall—they affect JavaScript's date calculations too. When clocks "spring forward" (losing an hour) or "fall back" (gaining an hour), date arithmetic can produce bizarre results. An hour might vanish entirely, or the same hour might appear twice.
What Happens During DST Transitions
When you add 24 hours to a date that crosses a DST boundary, you might not land exactly one day later. If DST begins at 2:00 AM (clocks jump to 3:00 AM), that day only has 23 hours. If DST ends (clocks fall back from 2:00 AM to 1:00 AM), that day has 25 hours.
Example scenario: Adding one day (86400000 milliseconds) to March 12, 2023 at 1:00 AM in a DST-observing timezone doesn't give you March 13 at 1:00 AM—it gives you March 13 at 2:00 AM, because that day lost an hour.
This gets worse with month arithmetic. Adding one month to January 31 can give you unexpected results when combined with DST transitions, because setMonth() operates on local time components.
Why This Matters
If you're calculating business hours, scheduling events, or computing durations that span DST boundaries, you can't assume every day has 24 hours. The arithmetic works with milliseconds (always exact), but the local time representation shifts unpredictably.
Think of it like this: DST is like a detour on a highway. You traveled the right distance in miles, but the exit signs don't match what your map predicted because the road itself changed.
Key Takeaway: Always be aware that DST transitions can cause date arithmetic to skip or duplicate hours—test calculations that span known DST boundaries and consider using UTC timestamps for duration calculations.