Recent findings suggest that "How many days between these two dates?" sounds like a one-liner. Subtract, divide by 86,400,000, done. It works in most tests you will write, which is exactly why it ships with a bug.

Industry observers note that I have been building small date and money calculators, and the date ones have produced more "wait, that's wrong" moments than anything else. Here are the four places it breaks, with outputs I ran myself (Node 24), and a version that does not.

Sources indicate that fine in a quick test. Now run it in a machine set to US Eastern time:.

March 1 to March 31 is 30 days. You got 29, and the same code on a server running UTC returns 30. The bug only shows up for customers in a time zone that changes its clocks.

In 2026 the US moved its clocks forward on March 8. That day is 23 hours long, so a span that crosses it is one hour short of a whole number of days. 29.958333... is 29 days plus 23 hours, which is what you get when you count the real elapsed time and not the calendar.

In a recent development, going the other way, clocks fell back on November 1, 2026. October 1 to November 5 comes out as 35.041666..., so Math.floor happens to give the right answer there and Math.round would too. That is the nasty part: the same function is right in autumn and wrong in spring.

Math.round hides the problem for local-midnight dates, but I would not rely on it. If one of your inputs has a time of day, or comes from a different zone, rounding will eventually pick the wrong side.

Say you read the dates from a form or an API as "2026-03-01". JavaScript treats a date-only ISO string as UTC midnight, but prints it in local time:.

Same-looking input, a different day on screen. Drop the T00:00 and anything that calls getDate() on it returns the 28th in New York. This is behind a lot of "my birthday indicates up a day early" reports.

Recent findings suggest that A calendar day count has nothing to do with clocks, so take clocks out of it. Pull out the year, month and day, build UTC timestamps from those with Date.UTC, and subtract. UTC has no daylight saving, so every day is exactly 86,400,000 ms.

According to recent reports, I ran it with TZ set to America/New_York, UTC and Asia/Karachi. All three printed 30. If your inputs are date strings, split them yourself (const [y, m, d] = s.split("-").map(Number)) and call Date.UTC(y, m - 1, d) directly, so no local-time conversion ever happens.

According to recent reports, the Math.round is only there to absorb floating point noise in the division. With UTC midnights the quotient is already a whole number.

According to recent reports, february 28 to March 1 is 1 day in 2026 and 2 days in 2024. The UTC approach gets that right for free, because it uses the real calendar. The place people go wrong is when they write their own leap year check. The rule has three parts:.

Forget the 100 and 400 rules and 1900 and 2100 come out wrong. Better still, do not write the check at all: let Date.UTC handle it, as above. A year from January 1, 2024 to January 1, 2025 is 366 days with that function, and 2026 to 2027 is 365.

Total days is the easy question. The question people really ask is "how long is that in years, months and days?", and here there is no single right answer, only a convention.

How long is January 31 to March 1, 2026? In total days it is 29. In months and days, you first have to decide what "one month after January 31" means, because February has no 31st. The usual convention (used by Python's dateutil, among others) clamps to the last day of the month: January 31 plus one month is February 28, and then one more day gets you to March 1. So the answer is 1 month, 1 day.

According to recent reports, the textbook method of borrowing days from the previous month, the way you did subtraction at school, goes wrong here. I wrote it first and got [0, 1, -2], a negative number of days. Clamping fixes it:.

I checked it against dateutil.relativedelta on seven date pairs, and the answers matched on all of them:.

The leap day rows are the awkward ones. Whether someone born on February 29 turns a year older on February 28 or March 1 is a legal question and differs by country, so the "right" output depends on your product.

If you want to compare your results against something while you test, there is a free date difference calculator you can use for that kind of sanity check. Newer runtimes are also adding the Temporal API, which is built to avoid most of the traps above. It was not available in the Node 24 build I tested on, so I stuck with plain Date.

Disclosure: I run MultiCalify, a free calculator site, and the link above points to it.

→ Read original article