How to calculate age or the days between two dates without being off by one
Table of Contents
Calculating age or the days between two dates looks like a subtraction and isn’t one: months have different lengths, leap years exist, and the final day sometimes counts and sometimes doesn’t. Those three things are what produce the classic off-by-one error — an extra day, or an extra year — in deadlines, ages and due dates. This guide covers the quick mental math, the exact method, and how to do it in Excel, JavaScript and PHP.
If you just want the answer, this tool does both operations instantly, no signup:
The quick version: current year minus birth year
For a rough age, one subtraction is enough: 2026 − 1994 = 32. But that figure is only the real age if the person has already had their birthday this year. If their birthday hasn’t arrived yet, they’re one year younger.
The full rule, in two steps:
- Subtract the years:
current year − birth year. - If this year’s birthday hasn’t happened yet, subtract 1 from the result.
That’s why “2026 minus 32” has two valid answers: someone who is 32 today was born in 1994 if they’ve already blown out this year’s candles, or in 1993 if they haven’t. Any decent calculator makes this adjustment automatically; the bare subtraction gets it wrong roughly half the time — every day of the year before the birthday.
Calculating exact age in years, months and days
When you need the precise age — for a document, a contract, or plain curiosity — the correct way is column-by-column subtraction, like long subtraction with borrowing but on a calendar:
- Days: subtract the days. If it doesn’t work, borrow a month (you add the days of the month before the end date, not a flat 30).
- Months: subtract the months (minus the borrowed one, if any). If it doesn’t work, borrow a year (+12 months).
- Years: subtract the remaining years.
A worked example: from March 15, 1994 to July 31, 2026.
- Days: 31 − 15 = 16 days ✓ (no borrowing needed)
- Months: 7 − 3 = 4 months
- Years: 2026 − 1994 = 32 years
Result: 32 years, 4 months and 16 days. The detail of “borrowing the previous month’s days” is what keeps this from being simple division: a borrowed month can be worth 28, 29, 30 or 31 days depending on which one it is. It’s exactly why “2 years and 3 months” and “820 days” aren’t interchangeable: they measure the same interval with units of different sizes.
How many days are there between two dates
Here the trap isn’t the months but whether the final day is included:
- Days elapsed (what calculators and programming languages return): from March 1 to March 10 there are 9 days, not 10. It’s the pure difference:
end date − start date. - Days occupied (holiday days, rental days, calendar days of a leave): you want both ends included, so add 1 to the previous result.
Mixing up these two conventions is the number-one source of miscounted deadlines. Before fighting the arithmetic, decide which of the two you need; the subtraction is the same, only the +1 changes.
Leap years need no trick at all if you count on the real calendar: February 29 is a day like any other and adds one more. They only become a problem when someone approximates with “one year = 365 days”: over long intervals the error accumulates at a rate of one day every four years. That’s why 26 years isn’t 26 × 365 = 9,490 days, but around 9,496 or 9,497 depending on how many leap days the interval crosses.
Adding or subtracting days from a date (deadlines and due dates)
The other calendar operation real life throws at you: “what date is 90 days from today?”. The mechanics are to consume whole months:
From July 31, 2026, 90 days forward:
- to August 31 → 31 days (59 left)
- to September 30 → 30 days (29 left)
- 29 days into October → October 29, 2026 (a Thursday)
Two details that bite:
- Adding months is not adding 30 days. “One month” from January 31 can’t give February 31; the standard convention (the same one Excel and programming languages use) snaps to the last valid day of the target month: February 28 or 29. Always double-check month boundaries when starting from the 29th, 30th or 31st.
- Calendar days ≠ business days. Everything in this article counts calendar days (all of them). If your deadline is in business days, the count depends on weekends and your country’s holidays, and no generic formula solves that without the actual holiday calendar.
How to do it in Excel or Google Sheets
With the start date in A1 and the end date in B1:
| What you want | Formula | Result for 1994-03-15 → 2026-07-31 |
|---|---|---|
| Days between the two | =B1-A1 | 11,826 |
| Whole years (age) | =DATEDIF(A1,B1,"Y") | 32 |
| Leftover months | =DATEDIF(A1,B1,"YM") | 4 |
| Leftover days | =DATEDIF(A1,B1,"MD") | 16 |
DATEDIF is the classic age function: it doesn’t show up in Excel’s autocomplete but works in every version and in Google Sheets. The direct subtraction =B1-A1 returns days elapsed — remember the +1 if your case is days occupied.
How to do it in code (JavaScript and PHP)
In JavaScript, the difference in days is a millisecond subtraction; for exact age a date library is the sane choice, but the by-hand version is short:
const start = new Date("1994-03-15");
const end = new Date("2026-07-31");
// Days elapsed (UTC, dodging DST changes)
const days = (end - start) / 86_400_000; // 11826
// Age in whole years, with the birthday adjustment
let age = end.getFullYear() - start.getFullYear();
const birthdayPending =
end.getMonth() < start.getMonth() ||
(end.getMonth() === start.getMonth() && end.getDate() < start.getDate());
if (birthdayPending) age--; // 32
In PHP the dirty work is already done: DateTime::diff() returns years, months, days and the day total in a single object:
$start = new DateTimeImmutable('1994-03-15');
$end = new DateTimeImmutable('2026-07-31');
$d = $start->diff($end);
// $d->y = 32, $d->m = 4, $d->d = 16, $d->days = 11826
If you work with Unix timestamps instead of calendar dates, the timestamp converter translates in both directions.
Summary
- Quick age: subtract the years, minus 1 if the birthday hasn’t arrived.
- Exact age: column subtraction, borrowing from the previous month (worth 28-31 days, not 30).
- Days between dates: decide up front whether the final day counts (
+1for days occupied). - Deadlines: adding months snaps to the last valid day; adding days is exact.
- Leap years: free if you count on the real calendar; one day of error every 4 years if you approximate with 365.
And if it’s a one-off calculation, the date and age calculator gives you both readings — years/months/days and totals — with nothing to install.