To calculate the date difference in days in TypeScript, subtract the two getTime() values and divide by 86,400,000, the milliseconds in one day. Wrap it in Math.round() so a daylight saving shift can’t knock the answer off by one:
const days = Math.round((end.getTime() - start.getTime()) / 86_400_000);
I ran all of this with TypeScript 7.0.2 and Node.js v22.22.2, strict mode on and the clock set to US Central Time, so the DST results match what a Chicago or Dallas server would print.
We’ll cover the basic helper, the compiler error you hit when subtracting dates directly, the DST trap, true calendar days with Date.UTC(), date-fns, and a few real tasks like counting business days.
Calculate the difference in days with getTime()
A Date stores a timestamp in milliseconds. getTime() returns that number, so the gap between two dates is plain subtraction:
const MS_PER_DAY = 1000 * 60 * 60 * 24;
function daysBetween(start: Date, end: Date): number {
return Math.round((end.getTime() - start.getTime()) / MS_PER_DAY);
}
const signed = new Date("2026-06-01");
const starts = new Date("2026-07-15");
console.log("June 1 -> July 15:", daysBetween(signed, starts));
console.log("July 15 -> June 1:", daysBetween(starts, signed));
console.log("absolute value: ", Math.abs(daysBetween(starts, signed)));
Output:
June 1 -> July 15: 44
July 15 -> June 1: -44
absolute value: 44

Math.abs() when order doesn’t matter.Strings like "2026-06-01" are parsed as UTC midnight, so both dates sit at the same clock time. If your dates arrive in other formats, see converting a string to a date in TypeScript first.
Why can’t I subtract two Date objects in TypeScript?
Plain JavaScript lets you write end - start because it quietly converts each date to a number. TypeScript refuses, since a Date isn’t a number type:
const checkIn = new Date("2026-11-20");
const checkOut = new Date("2026-11-27");
const nights = (checkOut - checkIn) / 86_400_000;
Output:
diff_subtract.ts(4,17): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
diff_subtract.ts(4,28): error TS2363: The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

Call getTime() on each side (or Number(date)) and the error goes away. The same rule applies when you compare dates in TypeScript with arithmetic.
Days between dates across daylight saving time
Dates built with new Date(year, month, day) use local time. In most of the US, clocks jumped forward on March 8, 2026, so that day only had 23 hours:
const MS_PER_DAY = 86_400_000;
const start = new Date(2026, 2, 1); // March 1, 2026, local midnight
const end = new Date(2026, 2, 15); // March 15, after DST starts on March 8
const raw = (end.getTime() - start.getTime()) / MS_PER_DAY;
console.log("time zone:", Intl.DateTimeFormat().resolvedOptions().timeZone);
console.log("raw days: ", raw);
console.log("floor: ", Math.floor(raw));
console.log("round: ", Math.round(raw));
function calendarDays(a: Date, b: Date): number {
const utcA = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utcB = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return (utcB - utcA) / MS_PER_DAY;
}
console.log("Date.UTC: ", calendarDays(start, end));
Output:
time zone: America/Chicago
raw days: 13.958333333333334
floor: 13
round: 14
Date.UTC: 14

Math.floor drops a whole day.That’s why I round instead of flooring, and you should too. The cleaner fix is the calendarDays helper above: it rebuilds each date with Date.UTC(), which has no daylight saving, so the division is always exact.
Count calendar days between two dates
Elapsed time and calendar days aren’t the same question. An order placed at 11:30 PM and shipped 45 minutes later crossed into a new day:
const MS_PER_DAY = 86_400_000;
function calendarDays(a: Date, b: Date): number {
const utcA = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utcB = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return (utcB - utcA) / MS_PER_DAY;
}
const ordered = new Date(2026, 10, 25, 23, 30); // Nov 25, 11:30 PM
const shipped = new Date(2026, 10, 26, 0, 15); // Nov 26, 12:15 AM
const elapsed = (shipped.getTime() - ordered.getTime()) / MS_PER_DAY;
console.log("elapsed days: ", elapsed.toFixed(4));
console.log("floor elapsed:", Math.floor(elapsed));
console.log("calendar days:", calendarDays(ordered, shipped));
Output:
elapsed days: 0.0313
floor elapsed: 0
calendar days: 1
Use elapsed time for things like “is this session older than 30 days”, and calendar days for due dates, reservations and “days until” counters. To move a date forward instead of measuring it, see adding days to a date in TypeScript.
Get the difference in days with date-fns
If your project already uses date-fns, it ships both kinds of difference. differenceInDays counts full 24-hour days, while differenceInCalendarDays ignores the time of day:
import { differenceInCalendarDays, differenceInDays } from "date-fns";
const ordered = new Date(2026, 10, 25, 23, 30); // Nov 25, 11:30 PM
const shipped = new Date(2026, 10, 27, 8, 0); // Nov 27, 8:00 AM
console.log("differenceInDays: ", differenceInDays(shipped, ordered));
console.log("differenceInCalendarDays:", differenceInCalendarDays(shipped, ordered));
Output:
differenceInDays: 1
differenceInCalendarDays: 2

Both functions take the later date first and return a negative number if you swap them. For displaying the dates themselves, I use the patterns in formatting dates in TypeScript.
Practical examples: days until, days late and business days
Here’s the calendar helper doing everyday work: a countdown to Thanksgiving, a late fee of $1.50 per day on an invoice, and a weekday count that skips Saturdays and Sundays:
const MS_PER_DAY = 86_400_000;
function calendarDays(a: Date, b: Date): number {
const utcA = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utcB = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return (utcB - utcA) / MS_PER_DAY;
}
function businessDays(a: Date, b: Date): number {
let count = 0;
const day = new Date(a.getFullYear(), a.getMonth(), a.getDate());
while (day < b) {
const dow = day.getDay();
if (dow !== 0 && dow !== 6) count++;
day.setDate(day.getDate() + 1);
}
return count;
}
const today = new Date(2026, 9, 14); // Oct 14, 2026
const thanksgiving = new Date(2026, 10, 26); // Nov 26, 2026
console.log("days until Thanksgiving:", calendarDays(today, thanksgiving));
const invoiceDue = new Date(2026, 8, 30); // Sep 30, 2026
const late = calendarDays(invoiceDue, today);
console.log(`invoice ${late} days late, fee $${(late * 1.5).toFixed(2)}`);
console.log("business days Oct 14 -> Oct 28:", businessDays(today, new Date(2026, 9, 28)));
Output:
days until Thanksgiving: 43
invoice 14 days late, fee $21.00
business days Oct 14 -> Oct 28: 10
The business-day loop doesn’t know about holidays; keep a Set of holiday date strings and skip those too. You can also start the countdown from the current date in TypeScript, or group results by week number.
Ways to calculate days between dates in TypeScript
| Approach | Counts | Watch out for |
|---|---|---|
getTime() diff + Math.round | Elapsed days | Never use Math.floor on local dates |
Date.UTC() on date parts | Calendar days | Uses the local date of each value |
date-fns differenceInDays | Full 24-hour days | Adds a dependency |
date-fns differenceInCalendarDays | Calendar days | Later date goes first |
If you work with dates and arrays a lot, you’ll like these next:
- Sort an array by date in TypeScript
- Filter arrays in TypeScript
- Sum an array in TypeScript
- Compare strings in TypeScript
Frequently asked questions
How do I get the number of days between two dates in TypeScript?
Subtract the getTime() values, divide by 86,400,000 and round: Math.round((b.getTime() - a.getTime()) / 86_400_000).
Why is my day count off by one?
Usually a daylight saving change or a time-of-day difference. Round instead of flooring, or compare calendar days with Date.UTC().
How do I get a positive number regardless of order?
Wrap the result in Math.abs(). Without it, an earlier end date returns a negative count.
Why does TypeScript reject date2 – date1?
Arithmetic needs number types, so strict TypeScript reports TS2362. Use date2.getTime() - date1.getTime().
Should I use date-fns or plain Date math?
Plain math is fine for simple counts. date-fns helps when you need calendar days, months or many date operations.
Where is Date.UTC() documented?
See Date.UTC() on MDN.

Bijay Kumar is a 13-time Microsoft MVP with more than 18 years in software development, and the founder of Python Guides and TSinfo Technologies. He started out building .NET and SharePoint solutions at HP, TCS and KPIT before moving into Python, machine learning and AI, and he also builds web apps with TypeScript and React. He writes the tutorials here himself, and every example is run before publishing so you see the real output. More about Bijay · Microsoft MVP profile · LinkedIn