Ask a JavaScript developer for someone's age and you will usually get this:

const age = Math.floor((Date.now() - birth.getTime()) / (365.25 * 24 * 60 * 60 * 1000)); 

It looks reasonable. It is wrong often enough to matter, and the reasons are worth understanding because the same three mistakes show up in every date calculation you will write.

Mistake one: a year is not 365.25 days

365.25 is an average. Individual years are 365 or 366 days, and the drift between the average and the actual year is up to a day. For a 20-year-old that error is small, but it is not zero, and near a birthday it flips the answer. Any formula that converts a date difference into years by division will be off for somebody, on some day, in production.

Mistake two: milliseconds are not calendar time

Subtracting two timestamps gives you elapsed physical time. Age is a calendar fact: it changes at midnight on your birthday in your local timezone, not 31,556,952,000 milliseconds after you were born. Daylight saving time adds or removes an hour, which is enough to make a birthday that falls on a DST boundary come out a day early or late.

Mistake three: month lengths and leap years

February has 29 days in leap years. A leap year is divisible by 4, except centuries, unless divisible by 400. If you compute months by dividing days by 30, or years by 12 months, February quietly breaks your result.

The fix: do calendar arithmetic, not timestamp arithmetic

Borrow the way you were taught to subtract numbers at school. Compare year, month and day separately, and borrow a month or a year when the day or month has not been reached yet:

function exactAge(birth, today = new Date()) { let years = today.getFullYear() - birth.getFullYear(); let months = today.getMonth() - birth.getMonth(); let days = today.getDate() - birth.getDate(); if (days < 0) { months -= 1; // days in the month before `today` const prevMonth = new Date(today.getFullYear(), today.getMonth(), 0); days += prevMonth.getDate(); } if (months < 0) { years -= 1; months += 12; } return { years, months, days }; } 

new Date(year, month, 0) gives the last day of the previous month, so getDate() on it is the number of days you borrow. That one line handles February, leap years, and 30 versus 31 day months without a lookup table.

The edge case that bites: 29 February

Someone born on 29 February has a birthday question every non-leap year. Pick a rule and be explicit about it. The common choices are 28 February (legal in most places) and 1 March. Whichever you pick, write it down in a comment, because the next developer will assume the other one.

// Birthdays on 29 Feb are counted on 28 Feb in non-leap years. function isBirthday(birth, today) { const m = birth.getMonth(), d = birth.getDate(); if (m === 1 && d === 29 && !isLeap(today.getFullYear())) { return today.getMonth() === 1 && today.getDate() === 28; } return today.getMonth() === m && today.getDate() === d; } function isLeap(y) { return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0; } 

Test the boundaries, not the happy path

Three tests catch nearly every bug in this code: a birthday that is tomorrow, a birthday that was yesterday, and 29 February. Add a fourth for a date near a DST change if your users are spread across timezones. If a test would pass on any day of the year, it is not testing the calendar.

A note on libraries

Date libraries handle this, and using one is fine. But the calculation above is fifteen lines, has no dependencies, and you can read every branch of it. For a small utility, that trade is usually worth it.

I built exactly this into my age calculator (https://ashraf-alrashidi.github.io/AgeCalculator/), where the interesting part was never the subtraction, it was deciding what a birthday means on 29 February.

This article was written with AI assistance and reviewed by Ashraf Alrashidi.