Emmanuel Akinfulubi

JavaScriptBasic

Calculate age in years, months and days in JavaScript

Advanced · 4 min read

Age checks are everywhere: sign-up forms that need you to be 18, insurance quotes, school admissions, a birthday app that says "29 years, 9 months and 26 days". Getting the age wrong by one year can let someone through a form they shouldn't pass, or block someone who should.

In this tutorial you will write ageOn(birth, on), which gives the exact age on a given date as years, months and days. The method is the same one you learned for subtraction on paper: subtract each column, and borrow when a column comes out negative. You need to know how to create a date and read its parts with getFullYear, getMonth and getDate. Months count from 0, so January is 0.

Why not just subtract the years?

Ada was born on 20 March 1996. On 15 January 2026:

JavaScript

const ada = new Date(1996, 2, 20);
const today = new Date(2026, 0, 15);
console.log(today.getFullYear() - ada.getFullYear()); // 30

But her birthday in 2026 hasn't happened yet. She is still 29. Subtracting the years ignores whether the birthday has passed.

Dividing the gap in milliseconds by the length of a year doesn't work either, because years are not all the same length.

Borrowing, as on paper

When you subtract 26 from 43 by hand, 3 minus 6 doesn't go, so you borrow 10 from the tens column. Dates work the same way, but the columns are years, months and days:

  • If the days come out negative, borrow one month, and add that month's number of days.
  • If the months come out negative, borrow one year, and add 12 months.

Here is the exercise. Try it yourself first if you like, or read on and we will build it together.

129 An age in years, months and days

hard

Given a date of birth and a date to measure on, report the age as whole years, months and days. Subtracting the years alone must not turn a 29-year-old into a 30-year-old two months before their birthday.

Stuck? Three hints, smallest first
  1. 1. Subtract the three parts separately, then fix up the ones that came out negative.
  2. 2. A negative day count borrows the length of the month before the measuring date.
  3. 3. A negative month count borrows twelve months from the year.

Step 1: subtract each part

Step 1

function ageOn(birth, on) {
  const years = on.getFullYear() - birth.getFullYear();
  const months = on.getMonth() - birth.getMonth();
  const days = on.getDate() - birth.getDate();
  return { years, months, days };
}
console.log(ageOn(new Date(1996, 0, 10), new Date(2026, 0, 15))); // { years: 30, months: 0, days: 5 }
console.log(ageOn(new Date(1996, 2, 20), new Date(2026, 0, 15))); // { years: 30, months: -2, days: -5 }

The first birthday, 10 January, has already passed, so no part is negative and the answer is right. Ada's comes out with negative months and days. That's the signal to borrow.

Step 2: borrow for the days

When days is negative, take one month away and add the number of days in the month before the measuring date. On 15 January, that month is December, which has 31 days.

new Date(year, month, 0) gives day 0 of a month, which is the last day of the month before. Its getDate() is that month's length:

Step 2

const on = new Date(2026, 0, 15);
console.log(new Date(on.getFullYear(), on.getMonth(), 0).getDate()); // 31: December 2025

So in Ada's case, days become -5 + 31 = 26, and months become -2 - 1 = -3.

Step 3: borrow for the months

Now months is -3. Take one year away and add 12 months: -3 + 12 = 9, and the years drop from 30 to 29.

Step 3

function ageOn(birth, on) {
  let years = on.getFullYear() - birth.getFullYear();
  let months = on.getMonth() - birth.getMonth();
  let days = on.getDate() - birth.getDate();
  if (days < 0) {
    months -= 1;
    days += new Date(on.getFullYear(), on.getMonth(), 0).getDate();   // days in the month before
  }
  if (months < 0) {
    years -= 1;
    months += 12;
  }
  return { years, months, days };
}
console.log(ageOn(new Date(1996, 2, 20), new Date(2026, 0, 15))); // { years: 29, months: 9, days: 26 }

The days are fixed first, because borrowing for days can push the months negative. Then the months are fixed. The variables use let instead of const because we change them.

Step 4: test the edges

Birthdays near the measuring date are where mistakes hide:

Step 4

function ageOn(birth, on) {
  let years = on.getFullYear() - birth.getFullYear();
  let months = on.getMonth() - birth.getMonth();
  let days = on.getDate() - birth.getDate();
  if (days < 0) {
    months -= 1;
    days += new Date(on.getFullYear(), on.getMonth(), 0).getDate();
  }
  if (months < 0) {
    years -= 1;
    months += 12;
  }
  return { years, months, days };
}
const TODAY = new Date(2026, 0, 15);
console.log(ageOn(new Date(2000, 0, 15), TODAY)); // { years: 26, months: 0, days: 0 }
console.log(ageOn(new Date(2000, 0, 16), TODAY)); // { years: 25, months: 11, days: 30 }
console.log(ageOn(TODAY, TODAY));                 // { years: 0, months: 0, days: 0 }
console.log(ageOn(new Date(2000, 1, 29), new Date(2026, 1, 28))); // { years: 25, months: 11, days: 30 }

On the birthday itself the age is a whole number of years. One day before, it's still the year before.

The last line is a person born on 29 February. 2026 has no 29 February, so on 28 February this code says they are still 25, and they turn 26 on 1 March. Some places count it the other way. There's no single right answer, so decide and write it down.

Notice the dates are passed in, rather than calling new Date() inside. That makes the function testable: you can pin "today" to any date you like.

The full solution

The answer panel has ageOn with both borrows, measured on a pinned date of 15 January 2026. It prints Ada's age and what year subtraction alone would say. Press Run to see the output.

The answer

function ageOn(birth, on) {
  let years = on.getFullYear() - birth.getFullYear();
  let months = on.getMonth() - birth.getMonth();
  let days = on.getDate() - birth.getDate();

  if (days < 0) {
    months -= 1;
    days += new Date(on.getFullYear(), on.getMonth(), 0).getDate();   // days in the month before
  }
  if (months < 0) {
    years -= 1;
    months += 12;
  }
  return { years, months, days };
}

const TODAY = new Date(2026, 0, 15);         // pinned, so this can be asserted at all
const ada = new Date(1996, 2, 20);           // 20 March 1996
console.log('Ada on 15 January 2026:', JSON.stringify(ageOn(ada, TODAY)));
console.log('year subtraction alone would say', TODAY.getFullYear() - ada.getFullYear());

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.

9 checks

test('birthday already passed this year', ageOn(new Date(1996, 0, 10), TODAY), { years: 30, months: 0, days: 5 });
test('birthday still to come, so the borrow happens', ageOn(ada, TODAY), { years: 29, months: 9, days: 26 });
test('and subtracting the years alone overstates it by one', [TODAY.getFullYear() - ada.getFullYear(), ageOn(ada, TODAY).years], [30, 29]);
test('a birthday on the day itself is a whole number of years', ageOn(new Date(2000, 0, 15), TODAY), { years: 26, months: 0, days: 0 });
test('the day before is still the year before', ageOn(new Date(2000, 0, 16), TODAY), { years: 25, months: 11, days: 30 });
test('a whole number of months', ageOn(new Date(1996, 2, 15), TODAY), { years: 29, months: 10, days: 0 });
test('a newborn is zero of everything', ageOn(TODAY, TODAY), { years: 0, months: 0, days: 0 });
test('the borrow uses December, so 31 days, not 30', ageOn(new Date(2000, 0, 20), TODAY).days, 26);
test('a 29 February birthday reads as not yet due on 28 February', ageOn(new Date(2000, 1, 29), new Date(2026, 1, 28)), { years: 25, months: 11, days: 30 });

Age is not a division. (now - birth) / 31536000000 is wrong because years are not all the same length, and subtracting the year numbers is wrong because it ignores whether the birthday has happened yet — the third assertion is that bug, and it is the one that puts an under-18 on the wrong side of a form. The correct shape is subtract each part, then borrow, exactly like long subtraction on paper.

Which month to borrow from is the detail people get wrong. new Date(on.getFullYear(), on.getMonth(), 0) is the last day of the month before the measuring date, so measuring in January borrows 31 days from December. Borrowing a flat 30, or borrowing from the birth month instead, shifts the answer by a day or two — small enough to survive review and large enough to matter on the day a tenancy or a loan term ends.

The last assertion has no objectively right answer. Someone born on 29 February 2000 has no birthday in 2026, and this code says they are still 25 on 28 February and turn 26 on 1 March. Different jurisdictions choose differently; the point is to choose deliberately and write it down, rather than discover your choice from a bug report.

Common mistakes

  • Subtracting only the years. Anyone whose birthday is still to come this year gets one year too many.
  • Borrowing a flat 30 days. Months have 28 to 31 days. Borrow the real length of the month before the measuring date.
  • Fixing the months before the days. Borrowing for days can make the months negative, so days must come first.

Try it yourself

  1. Write isAdult(birth, on) that returns true when the age is at least 18 years.
  2. Format the result as a sentence: '29 years, 9 months and 26 days'. Use "1 year" (singular) when the number is 1.
  3. Work out how many days are left until the next birthday after a given date.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.