Emmanuel Akinfulubi

JavaScriptDate

Count the days between two dates in JavaScript

Intermediate · 4 min read

"12 days until the deadline", "you stayed 3 nights", "posted 30 days ago": counting the days between two dates is everywhere. It looks like simple subtraction, and that is exactly why it so often ships with bugs. Two dates a few minutes apart, either side of midnight, should be one day apart. Plain subtraction says zero.

In this tutorial you will write daysBetween(from, to). It counts calendar days and ignores the time of day. It gets month ends and leap years right, and it gives a negative answer when to is before from. You need to know how to make a date with new Date(year, month, day, hour, minute) and that months count from 0.

Subtracting dates gives milliseconds

When you subtract one date from another, JavaScript turns both into their timestamps (milliseconds since 1970) and subtracts the numbers:

JavaScript

const a = new Date(2026, 0, 1);
const b = new Date(2026, 0, 2);
console.log(b - a); // 86400000

That number is 24 hours × 60 minutes × 60 seconds × 1000 milliseconds. Call it MS_PER_DAY. Dividing by it turns milliseconds into days.

Elapsed time is not calendar days

Subtraction measures elapsed time: how long passed between two moments. "How many days between" usually means something else: how many times the calendar date changed. Those two answers differ whenever the clock times don't match.

There is a second problem. In places that move their clocks for daylight saving, one day a year has 23 hours and another has 25. In New York in 2026, clocks went forward on 8 March, so midnight on 8 March to midnight on 9 March is only 23 hours. Divide that by MS_PER_DAY and you get about 0.96, not 1.

The fix for both problems is UTC, the world's reference time. UTC never changes its clocks, so every UTC day is exactly 24 hours. Date.UTC(year, month, day) gives the timestamp for midnight UTC on that date.

Here is the exercise. Try it on your own first if you like, then read on and we will build it together.

26 How many days between two dates?

fair

Count the days from one date to another, ignoring the time of day. Get it right across a month end, across a leap February, and in both directions.

Stuck? Three hints, smallest first
  1. 1. Strip the time first, or 23:59 to 00:01 the next morning will count as zero days.
  2. 2. Convert both to Date.UTC(year, month, day), where every day is exactly 24 hours long.
  3. 3. That makes the division exact, so no rounding is needed at all.

Step 1: the wrong turn

Start with the version most people write first:

Step 1

const MS_PER_DAY = 86400000;
const lateEvening = new Date(2026, 0, 1, 23, 59);
const nextMorning = new Date(2026, 0, 2, 0, 1);
const rawFloor = Math.floor((nextMorning - lateEvening) / MS_PER_DAY);
console.log(nextMorning - lateEvening); // 120000
console.log(rawFloor);                  // 0

The two moments are two minutes (120,000 milliseconds) apart. Math.floor rounds down, so the answer is 0 days. But the calendar went from 1 January to 2 January, which is one day.

Step 2: keep only the calendar date

Throw the time away. Take the date's local year, month and day, and turn them into midnight UTC on that date:

Step 2

const utcDay = (date) => Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());
console.log(utcDay(lateEvening) === utcDay(new Date(2026, 0, 1))); // true

23:59 on 1 January and midnight on 1 January give the same number, because only the year, month and day go in. Every date on the same calendar day maps to one timestamp.

Step 3: subtract and divide

Now subtract two of those timestamps and divide by the length of a day:

Step 3

const daysBetween = (from, to) => (utcDay(to) - utcDay(from)) / MS_PER_DAY;
console.log(daysBetween(lateEvening, nextMorning)); // 1
console.log(daysBetween(new Date(2026, 0, 1), new Date(2026, 0, 31))); // 30

Both timestamps are UTC midnights, and UTC days are always 24 hours, so the division is exact. You get a whole number every time, with no rounding needed.

Step 4: month ends and leap years

The calendar does the hard work. You don't need to know how long any month is:

Step 4

console.log(daysBetween(new Date(2026, 0, 31), new Date(2026, 2, 1))); // 29
console.log(daysBetween(new Date(2024, 0, 31), new Date(2024, 2, 1))); // 30
console.log(daysBetween(new Date(2026, 0, 1), new Date(2027, 0, 1)));  // 365
console.log(daysBetween(new Date(2024, 0, 1), new Date(2025, 0, 1)));  // 366

February 2026 has 28 days and February 2024 has 29, and the answers reflect that. A common year is 365 days and a leap year is 366.

Step 5: both directions

Swap the arguments and the answer turns negative:

Step 5

console.log(daysBetween(new Date(2026, 0, 31), new Date(2026, 0, 1)));  // -30
console.log(daysBetween(new Date(2026, 0, 15), new Date(2026, 0, 15))); // 0

A negative result means to is before from. If you only care about the distance, wrap the call in Math.abs.

The full solution

The panel has utcDay and daysBetween, plus the raw subtraction for comparison. It checks month ends, both kinds of February, both directions, whole years and the two minutes around midnight. Press Run to see the output.

The answer

const MS_PER_DAY = 86400000;

// Take the local calendar date and re-express it as UTC midnight. UTC has no
// daylight saving, so the subtraction below is an exact number of days.
const utcDay = (date) => Date.UTC(date.getFullYear(), date.getMonth(), date.getDate());

const daysBetween = (from, to) => (utcDay(to) - utcDay(from)) / MS_PER_DAY;

// What happens if you subtract the raw instants instead.
const lateEvening = new Date(2026, 0, 1, 23, 59);
const nextMorning = new Date(2026, 0, 2, 0, 1);
const rawFloor = Math.floor((nextMorning - lateEvening) / MS_PER_DAY);

console.log('01/01/2026 to 31/01/2026 :', daysBetween(new Date(2026, 0, 1), new Date(2026, 0, 31)));
console.log('31/01/2026 to 01/03/2026 :', daysBetween(new Date(2026, 0, 31), new Date(2026, 2, 1)));
console.log('two minutes apart, over midnight: dates', daysBetween(lateEvening, nextMorning), 'vs raw floor', rawFloor);

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('the first to the last of January', daysBetween(new Date(2026, 0, 1), new Date(2026, 0, 31)), 30);
test('across a 28-day February', daysBetween(new Date(2026, 0, 31), new Date(2026, 2, 1)), 29);
test('across a 29-day one', daysBetween(new Date(2024, 0, 31), new Date(2024, 2, 1)), 30);
test('the same day is zero', daysBetween(new Date(2026, 0, 15), new Date(2026, 0, 15)), 0);
test('the time of day is ignored', daysBetween(lateEvening, nextMorning), 1);
test('whereas subtracting the instants and flooring says zero', rawFloor, 0);
test('reversing the arguments negates the answer', daysBetween(new Date(2026, 0, 31), new Date(2026, 0, 1)), -30);
test('a common year is 365 days', daysBetween(new Date(2026, 0, 1), new Date(2027, 0, 1)), 365);
test('a leap year is 366', daysBetween(new Date(2024, 0, 1), new Date(2025, 0, 1)), 366);

Two mistakes are being avoided at once. Subtracting the raw instants counts elapsed time, not calendar days, so two minutes either side of midnight floors to zero — that assertion is the bug most "days until" counters ship with. And dividing local milliseconds by 86,400,000 goes wrong in any zone with daylight saving, where two days a year are 23 or 25 hours long.

Normalising both ends to Date.UTC(y, m, d) fixes both. The time is discarded deliberately, and the division is exact rather than rounded, which is a stronger position than reaching for Math.round and hoping.

Common mistakes

  • Subtracting the dates and flooring. That counts elapsed time, so 23:59 to 00:01 is zero days.
  • Dividing local timestamps by 86,400,000. In a zone with daylight saving, some days are 23 or 25 hours, so you get fractions like 0.96.
  • Reaching for Math.round to hide the fractions. It can paper over the problem, but normalizing to Date.UTC removes it.

Try it yourself

  1. Write daysUntil(target, today) that returns how many days are left until target. What should it return on the day itself?
  2. Count the days from 1 January 2026 to 25 December 2026.
  3. Write nightsStayed(checkIn, checkOut) for a hotel booking and test it with a check-in at 15:00 and a check-out at 11:00 two days later.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.