"How many days until 24 December?" sounds like a one-liner. In JavaScript it's a small trap with three jaws: time zones, daylight saving time and the way Date parses strings. I ran into all three while building a countdown app, so here's what goes wrong and the small set of helpers I ended up trusting.
All snippets below were run in Node 20. The Temporal one uses the @js-temporal/polyfill package.
Pitfall 1: a day is not always 86,400,000 ms
The classic approach:
const a = new Date ( 2026 , 2 , 28 ); // 28 March 2026, local midnight const b = new Date ( 2026 , 2 , 30 ); // 30 March 2026, local midnight console . log (( b - a ) / 86400000 );
Enter fullscreen mode
Exit fullscreen mode
In a European time zone (I ran it with TZ=Europe/Warsaw ) this prints:
1.9583333333333333
Enter fullscreen mode
Exit fullscreen mode
Clocks went forward on 29 March, so that local "day" was only 23 hours long. Depending on whether you then use Math.floor , Math.ceil or nothing, you get 1, 2 or a fraction.…