All articles

Age calculation: why it's not just subtracting years

A simple way to calculate age is subtracting the birth year from the current year. But this approach gives the wrong answer for anyone whose birthday hasn't happened yet this year — which matters more than it sounds, since a single day can decide whether someone is legally old enough to vote or to buy a drink.

Why naive year subtraction gets it wrong

If someone was born on December 15, 1990, and today is January 1, 2024, the naive calculation 2024 - 1990 gives 34 — even though they won't actually turn 34 for almost another year. An accurate calculation has to check whether this year's birthday has already happened, not just the difference in years.

The correct algorithm

First compute the difference in years, then compare the birth month and day to the current month and day. If this year's birthday hasn't happened yet (the month is earlier, or the month matches but the day is earlier), subtract one from the result.

February 29 as a special case

For people born on February 29, non-leap years usually treat their official birthday as either February 28 or March 1, depending on the context — one of the edge cases that's easy to miss when writing age-calculation code.

Why you'd need this

  • Accurately calculating age for signup forms with a minimum-age check.
  • Verifying your own code handles edge cases correctly (February 29, birthday today).
  • Quickly getting an exact age in years, months, and days from a birth date.

One birthday, two different legal ages

In the US, turning 18 and turning 21 are both legally meaningful, but for different things — 18 is the voting age nationwide, while 21 is the minimum drinking age in every state under the federal Minimum Drinking Age Act. The same birth date produces two separate "legal adulthood" milestones years apart, which is worth remembering if an age-gated form only checks for one threshold.

Try the tool