Calculate age from date of birth in JavaScript
Intermediate · 4 min read
Sign-up forms check that you are over 18. Insurance quotes, school admissions and profile pages all show an age. Working out someone's age from their date of birth sounds like one subtraction, but the simple version is wrong for part of every year.
In this tutorial you will write ageOn(birth, on). It returns someone's age in whole years on a given day. You will see the bug in the simple version, fix it with one check, and look at the tricky case of a birthday on 29 February. You need to know how to make a date with new Date(year, month, day), that months count from 0, and that getFullYear(), getMonth() and getDate() read the parts of a date.
The idea
Your age goes up by one on your birthday, not on 1 January. So the recipe has two parts:
- Subtract the birth year from the current year.
- If this year's birthday hasn't happened yet, take one off.
Part 2 is the one people forget.
Why the date is an argument
You could read today's date inside the function with new Date(). Then the answer would change every day, and you couldn't test it: a check written today would fail next year. Passing the day in as on makes the function give the same answer every time for the same inputs. You can still call it with new Date() when you want today's age.
Here is the exercise. Have a go yourself first if you like, then read on and we will build it step by step.
27 Age in whole years
fairWork out someone's age on a given day from their date of birth. Subtracting the years is not enough — the birthday may not have happened yet.
Stuck? Three hints, smallest first
- 1. Start with the difference in years, then take one off if the birthday is still to come.
- 2. "Still to come" means an earlier month, or the same month and an earlier day.
- 3. Take the "on" date as an argument. A function that reads the clock itself cannot be tested.
Step 1: subtract the years
Start with the simple version:
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
Month 5 is June, because months count from 0. 2026 minus 1990 is 36. But on 15 January 2026 this person hasn't had their birthday yet. They are still 35. The simple version is a year too high until 15 June.
Step 2: has the birthday happened yet?
The birthday is "still to come" in two situations:
- the current month is earlier than the birth month, or
- it is the same month, and the current day is earlier than the birth day.
In JavaScript, "or" is || and "and" is &&:
const birthdayStillToCome =
on.getMonth() < birth.getMonth() ||
(on.getMonth() === birth.getMonth() && on.getDate() < birth.getDate());
console.log(birthdayStillToCome); // true
January (0) is before June (5), so the first part is already true.
Step 3: put it in a function
Start from the year difference and take one off when the birthday is still to come. age-- subtracts one from age, which is why it is declared with let:
function ageOn(birth, on) {
let age = on.getFullYear() - birth.getFullYear();
const birthdayStillToCome =
on.getMonth() < birth.getMonth() ||
(on.getMonth() === birth.getMonth() && on.getDate() < birth.getDate());
if (birthdayStillToCome) age--;
return age;
}
console.log(ageOn(birth, new Date(2026, 0, 15))); // 35
Notice that the check only compares months and days. It never compares timestamps. A timestamp includes the time of birth and depends on the time zone, and neither should change how old someone is.
Step 4: test the days around the birthday
The interesting days are just before, on and just after the birthday:
console.log(ageOn(birth, new Date(2026, 5, 14))); // 35
console.log(ageOn(birth, new Date(2026, 5, 15))); // 36
console.log(ageOn(birth, new Date(2026, 5, 16))); // 36
console.log(ageOn(birth, new Date(2026, 4, 30))); // 35
console.log(ageOn(birth, birth)); // 0
On 14 June the day is earlier than 15, so one comes off. On 15 June neither condition is true, so the age goes up. 30 May is in an earlier month, so it is still 35. On the day of birth, the age is zero.
Step 5: born on 29 February
In a year without 29 February, when does a leap-day baby turn a year older?
const leapBirth = new Date(2004, 1, 29); // 29 February 2004
console.log(ageOn(leapBirth, new Date(2026, 1, 28))); // 21
console.log(ageOn(leapBirth, new Date(2026, 2, 1))); // 22
On 28 February 2026 the month matches and 28 is less than 29, so the birthday counts as still to come. On 1 March the month is later, so it has passed. This function treats 1 March as the birthday in common years. Some systems treat 28 February that way instead. There is no single right answer, so whichever your app needs, choose it on purpose and test it.
The full solution
The panel has ageOn with a birth date of 15 June 1990. It checks the days around the birthday, an earlier month, the day of birth and the 29 February case. It also prints the age today, which is shown but never checked, because it changes. Press Run to see the results.
function ageOn(birth, on) {
let age = on.getFullYear() - birth.getFullYear();
const birthdayStillToCome =
on.getMonth() < birth.getMonth() ||
(on.getMonth() === birth.getMonth() && on.getDate() < birth.getDate());
if (birthdayStillToCome) age--;
return age;
}
const birth = new Date(1990, 5, 15); // 15 June 1990
console.log('on 15/01/2026:', ageOn(birth, new Date(2026, 0, 15)));
console.log('on 14/06/2026:', ageOn(birth, new Date(2026, 5, 14)), '<- day before the birthday');
console.log('on 15/06/2026:', ageOn(birth, new Date(2026, 5, 15)), '<- on the day');
console.log('today :', ageOn(birth, new Date()), '<- shown, never asserted');
Output
8 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 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('before the birthday in that year', ageOn(birth, new Date(2026, 0, 15)), 35);
test('the day before it', ageOn(birth, new Date(2026, 5, 14)), 35);
test('on the day itself, the age goes up', ageOn(birth, new Date(2026, 5, 15)), 36);
test('and stays up the day after', ageOn(birth, new Date(2026, 5, 16)), 36);
test('an earlier month counts as still to come', ageOn(birth, new Date(2026, 4, 30)), 35);
test('on the day of birth, the age is zero', ageOn(birth, birth), 0);
test('someone born on 29 February is 21 on 28 February 2026', ageOn(new Date(2004, 1, 29), new Date(2026, 1, 28)), 21);
test('and 22 on 1 March', ageOn(new Date(2004, 1, 29), new Date(2026, 2, 1)), 22);
on.getFullYear() - birth.getFullYear() is right for roughly half the year and a year too high for the other half. The correction is the whole exercise, and note that it compares month and day only — never the timestamps, which would drag the time of birth and the time zone into an answer that should not depend on either.
The two 29 February assertions show the edge case without pretending there is one right answer: this implementation treats the birthday as having passed on 1 March in a common year, which is the usual reading and matches English law. Some jurisdictions say 28 February. Whichever you pick, pick it deliberately.
Common mistakes
- Subtracting the years and stopping. The answer is a year too high until the birthday comes round.
- Comparing only the month. In the birth month itself, you also need to check the day.
- Dividing milliseconds by the length of a year. Years are 365 or 366 days long, so the result can be wrong on or near a birthday.
Try it yourself
- Write
isAdult(birth, on)that returnstruefrom someone's 18th birthday onwards. - Change
ageOnso a 29 February birthday counts as passed on 28 February in common years. Test both dates in 2026. - Write
nextBirthday(birth, on)that returns the date of the next birthday on or afteron.