Utilix knowledge base
How to Calculate the Difference Between Two Dates
Published Apr 17, 2026
Date arithmetic is deceptively tricky — months have different lengths, leap years add an extra day, and time zones can shift dates by a day. This guide covers how to calculate date differences correctly in each unit.
Days Between Two Dates
The simplest form: count calendar days from date A to date B.
Method: Convert both dates to a day count from a common epoch (e.g., Julian Day Number or Unix timestamp) and subtract:
Days = (End date − Start date) in milliseconds ÷ (1000 × 60 × 60 × 24)
In JavaScript:
const start = new Date("2026-01-01");
const end = new Date("2026-04-18");
const days = (end - start) / (1000 * 60 * 60 * 24); // 107
Leap years: If the range spans 29 February, those days are already included in the calendar count automatically.
Weeks Between Dates
Weeks = Math.floor(Days ÷ 7)
Remaining days = Days % 7
Example: 107 days = 15 weeks + 2 days
Months Between Dates
Months are irregular (28–31 days), so you cannot simply divide days by 30.
Correct method:
- Count the difference in months:
(endYear × 12 + endMonth) − (startYear × 12 + startMonth) - If the end day is before the start day, subtract 1
Example: 1 Nov 2025 → 18 Apr 2026:
- (2026×12 + 4) − (2025×12 + 11) = 24316 − 24311 = 5 months
- Day 18 ≥ day 1? Yes → 5 months, 17 days
Years, Months, Days (ISO Duration)
For a complete breakdown:
- Start with the year difference.
- If the anniversary hasn't occurred yet this year, subtract 1 year, add 12 months.
- Calculate remaining months (same logic).
- Calculate remaining days.
Example: 15 June 2020 → 18 April 2026:
- Years: 2026 − 2020 = 6 → but April 18 < June 15, so 5 complete years
- Remaining: June 15, 2025 → April 18, 2026 = 10 months, 3 days
- Result: 5 years, 10 months, 3 days
Including or Excluding the Start/End Day
The convention matters in legal and business contexts:
- Inclusive: Count both start and end day → add 1 to the day count
- Exclusive: Count only days in between → standard mathematical subtraction
- Business days: Exclude Saturdays, Sundays (and optionally public holidays)
Example: "How many days from 1 April to 10 April?"
- Exclusive: 9 days (the difference)
- Inclusive: 10 days (April 1, 2, 3 ... 10)
For deadlines, legal notices, and contracts — clarify which convention applies.
Working Days (Business Days)
Excluding weekends:
Business days = Total calendar days
− (Complete weeks × 2)
− Partial weekend days at start/end
A more precise approach counts each day and checks getDay() !== 0 && getDay() !== 6. Public holiday calendars vary by country.
Time Zones and Date Differences
If the start date is in one timezone and the end date is in another (or if the current time straddles midnight differently in two zones), the result can differ by ±1 day.
Best practice: Convert both dates to UTC before subtracting, or use a timezone-aware library like luxon.
Use the Date Difference Calculator to find the exact duration between any two dates in years, months, days, and total days.