General Math
How to Calculate Days Between Two Dates (and Why It's Harder Than You Think)
The math behind date difference calculation. Covers calendar arithmetic, business days, leap years, and when you'd actually need an exact date gap.
“How many days until my vacation?” “How long have I been at this job?” “How many business days between these two project milestones?” Date arithmetic sounds simple — until you encounter months with different lengths, leap years, and the question of whether to include the start date.
Total days: the simple case
The easiest approach: convert both dates to a common reference (milliseconds since epoch) and subtract:
days = floor((date2 - date1) / 86400000)
This gives you the absolute gap in 24-hour periods. No calendar quirks — just pure elapsed time.
Calendar difference (years, months, days)
This is trickier. “From January 31 to March 1” — is that 1 month and 1 day? Or 1 month and 0 days? Different systems disagree.
The standard approach:
- Subtract years
- Subtract months (borrowing a year if negative)
- Subtract days (borrowing the previous month’s length if negative)
This gives a human-readable “2 years, 3 months, 15 days” breakdown.
Business days
Most people asking “days between dates” actually want working days — excluding weekends and optionally public holidays.
Business days = Total days − Saturdays − Sundays − Holidays
Counting Saturdays/Sundays in a range: iterate through the dates and skip weekends. For public holidays, you need a country-specific calendar.
Leap year complications
February 29 exists only in leap years (divisible by 4, except centuries not divisible by 400). A range spanning Feb 28–Mar 1 is 1 day in leap years and also 1 day in non-leap years — but the total days in the full year are different (365 vs 366).
Common use cases
- Employment tenure — HR departments calculate exact service years
- Loan tenure — Banks need exact days for interest accrual
- Project management — Sprint durations, deadline countdown
- Legal — Notice periods, statute of limitations, contract durations
- Personal — Pregnancy due dates, event countdowns
Calculate any date gap with the OurDailyCalc date difference calculator — it shows years/months/days, total days, weeks, hours, and business days.
TL;DR
- Simple days: subtract timestamps, divide by 86400000
- Calendar breakdown uses month-borrowing arithmetic
- Business days exclude weekends (and optionally holidays)
- Leap years add an extra day but the formula handles it automatically
OurDailyCalc Team
OurDailyCalc — beautiful tools for everyday calculations.