Tech
Counting days between two dates in JavaScript without off-by-one bugs
"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:
...
Read the full discussion on Dev.to
This article was aggregated from Dev.to. Click to join the conversation.
View on Dev.to