Exact age in years, months and days in JavaScript
Advanced · 4 min read
"You are 35 years, 7 months and 0 days old." Baby trackers, pet records, employment histories and insurance forms often need an age broken into years, months and days, not just whole years. It is a nice puzzle, because months have different lengths.
In this tutorial you will write ageParts(birth, on), which returns { years, months, days } between two dates. For example, 15 June 1990 to 15 January 2026 is 35 years, 7 months and 0 days. You need to know getFullYear(), getMonth() and getDate(), and that months count from 0.
Subtraction with borrowing
Remember long subtraction from school? When a column goes negative, you borrow from the column to its left. To work out 52 minus 17, you get 2 minus 7 in the ones column, so you borrow ten from the tens.
Dates work the same way, with three columns: years, months and days. Subtract each one on its own. If days go negative, borrow a month. If months go negative, borrow a year. The only difference is how much you borrow:
- One year is always 12 months.
- One month is 28 to 31 days, depending on which month you borrow.
Which month do you borrow?
Think about counting from 20 December to 15 January. From 20 December to 20 January would be one month. We stopped 5 days short of that, in the month before the later date, December. So the days you borrow are December's 31. That rule, "borrow the month before the later date", is the line most attempts get wrong.
Here is the exercise. Try it on your own first if you like, then read on and we will build it step by step.
52 Age in years, months and days
hardBreak an age into years, months and days: 15 June 1990 to 15 January 2026 is 35 years, 7 months and 0 days.
Stuck? Three hints, smallest first
- 1. Subtract each field on its own, then fix up the negatives from the smallest upwards.
- 2. When the days come out negative, borrow the length of the month before the later date.
- 3.
new Date(year, month, 0).getDate()is the length of the previous month.
Step 1: subtract each column
const birth = new Date(1990, 5, 15); // 15 June 1990
const on = new Date(2026, 0, 15); // 15 January 2026
console.log(on.getFullYear() - birth.getFullYear()); // 36
console.log(on.getMonth() - birth.getMonth()); // -5
console.log(on.getDate() - birth.getDate()); // 0
36 years and minus 5 months. The months column went negative because January comes before June.
Step 2: borrow a year
Borrowing one year gives 12 months to the months column:
let years = 36;
let months = -5;
if (months < 0) {
years--;
months += 12;
}
console.log(years, months); // 35 7
35 years and 7 months. That matches the example.
Step 3: when the days go negative
Now try a birthday on the 20th:
const later = new Date(1990, 5, 20); // 20 June 1990
console.log(on.getDate() - later.getDate()); // -5
The days column is -5. We need to borrow a month, and add that month's number of days.
Step 4: the length of the month before
There is a neat trick for the length of a month: day 0 of a month is the last day of the month before it. So new Date(year, month, 0).getDate() is the length of the previous month:
console.log(new Date(2026, 0, 0).getDate()); // 31
console.log(new Date(2026, 2, 0).getDate()); // 28
Day 0 of January 2026 is 31 December 2025, so December has 31 days. Day 0 of March 2026 is 28 February.
Passing on's year and month gives the length of the month before on, which is exactly the month we need to borrow.
Step 5: put it together
Borrow days first, then months, from the smallest column upwards:
function ageParts(birth, on) {
let years = on.getFullYear() - birth.getFullYear();
let months = on.getMonth() - birth.getMonth();
let days = on.getDate() - birth.getDate();
if (days < 0) {
months--;
days += new Date(on.getFullYear(), on.getMonth(), 0).getDate();
}
if (months < 0) {
years--;
months += 12;
}
return { years, months, days };
}
console.log(ageParts(birth, on)); // { years: 35, months: 7, days: 0 }
console.log(ageParts(later, on)); // { years: 35, months: 6, days: 26 }
For the 20 June birthday, the days were -5. Borrowing December's 31 days gives 26, and the months drop by one more, to 6.
The order matters. Borrowing a day can push the months negative, so the month fix must come after the day fix.
Step 6: the wrong turn, borrowing a flat 30
Some versions borrow 30 days every time:
console.log(15 - 20 + 30); // 25
That gives 25 days, one short, because December has 31. A flat 30 is wrong for 8 of the 12 months.
Step 7: the leap-day edge
const leapBirth = new Date(2000, 1, 29); // 29 February 2000
console.log(ageParts(leapBirth, new Date(2026, 2, 1))); // { years: 26, months: 0, days: 0 }
console.log(ageParts(leapBirth, new Date(2026, 1, 28))); // { years: 25, months: 11, days: 30 }
To 1 March 2026, the borrow gives exactly 26 years. To 28 February, the days go to -1, the function borrows January's 31 days, and the answer is 25 years, 11 months and 30 days. You could argue it should be 26 years, since 28 February is the last day of that February. People disagree about this case, so what matters is that the function is consistent and you know what it does.
The full solution
The panel has ageParts. It prints the two examples from this lesson, plus an age as of today, which is shown but never checked. The checks cover the same day, a new-year gap, a whole month, one day short of a year and both leap-day cases. Press Run to see them.
function ageParts(birth, on) {
let years = on.getFullYear() - birth.getFullYear();
let months = on.getMonth() - birth.getMonth();
let days = on.getDate() - birth.getDate();
if (days < 0) {
months--;
// Borrow from the month before `on`: day 0 of that month is its last day.
days += new Date(on.getFullYear(), on.getMonth(), 0).getDate();
}
if (months < 0) {
years--;
months += 12;
}
return { years, months, days };
}
const birth = new Date(1990, 5, 15); // 15 June 1990
console.log('15/06/1990 to 15/01/2026:', ageParts(birth, new Date(2026, 0, 15)));
console.log('20/06/1990 to 15/01/2026:', ageParts(new Date(1990, 5, 20), new Date(2026, 0, 15)));
console.log('today :', ageParts(birth, new Date()), '<- shown, never asserted');
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.
test('35 years and 7 months exactly', ageParts(birth, new Date(2026, 0, 15)), { years: 35, months: 7, days: 0 });
test('a later birth day forces a borrow', ageParts(new Date(1990, 5, 20), new Date(2026, 0, 15)), { years: 35, months: 6, days: 26 });
test('the day of birth is all zeroes', ageParts(birth, birth), { years: 0, months: 0, days: 0 });
test('one day either side of new year', ageParts(new Date(2025, 11, 31), new Date(2026, 0, 1)), { years: 0, months: 0, days: 1 });
test('a whole month', ageParts(new Date(2026, 0, 1), new Date(2026, 1, 1)), { years: 0, months: 1, days: 0 });
test('a day short of a year', ageParts(new Date(2026, 0, 15), new Date(2027, 0, 14)), { years: 0, months: 11, days: 30 });
test('a leap-day birth measured to 1 March', ageParts(new Date(2000, 1, 29), new Date(2026, 2, 1)), { years: 26, months: 0, days: 0 });
test('and to 28 February, which borrows a whole January', ageParts(new Date(2000, 1, 29), new Date(2026, 1, 28)), { years: 25, months: 11, days: 30 });
test('the years agree with the simple whole-years calculation', ageParts(birth, new Date(2026, 5, 14)).years, 35);
Field-by-field subtraction with a borrow from the smallest unit upwards — the same shape as long subtraction on paper. The borrow has to take the length of the month before the later date, not of the birth month and not a flat 30, and that is the line most implementations get wrong.
The result is genuinely ambiguous at the edges and no arithmetic will settle it. 29 February 2000 to 28 February 2026 comes out here as 25 years, 11 months and 30 days, because the day count goes negative and borrows January's 31 days; you could equally argue for 26 years exactly, on the grounds that 28 February is the last day of that month. This implementation is consistent and documented, which is the most you can ask of a definition people disagree about.
Common mistakes
- Borrowing a flat 30 days. December, January and five other months have 31, and February has 28 or 29. Use day 0 of
on's month. - Borrowing from the birth month. The days to borrow come from the month before the later date.
- Fixing months before days. A day borrow can make the months negative, so do days first.
Try it yourself
- Write
formatAge(parts)that returns "35 years, 7 months and 1 day", with the right plurals. - Use
agePartsto show how long ago a company was founded on 1 March 2015, as of 15 January 2026. - Check that
ageParts(a, b).yearsalways matches a plain whole-years age calculation for a few dates.