DEV Community

Ashley Chen
Ashley Chen

Posted on Edited on

Age Arithmetic: Why "Years, Months, Days" Is Harder Than It Looks

Ask a person how old they are and you get a number. Ask a computer to compute it from two dates and you get a surprisingly subtle problem, because years and months are not fixed-length units. The naive endYear - startYear is wrong for almost everyone, and the fix is not a one-line change.

The problem with subtraction

The tempting implementation:

function age(start, end) {
  return end.getFullYear() - start.getFullYear();
}
Enter fullscreen mode Exit fullscreen mode

This is wrong whenever the anniversary has not happened yet in the end year. Someone born 2000-11-30 asked on 2026-11-14 is 25, not 26, and the naive version says 26. The correction is a single conditional:

function age(start, end) {
  let years = end.getFullYear() - start.getFullYear();
  const beforeAnniversary =
    end.getMonth() < start.getMonth() ||
    (end.getMonth() === start.getMonth() && end.getDate() < start.getDate());
  if (beforeAnniversary) years -= 1;
  return years;
}
Enter fullscreen mode Exit fullscreen mode

Months and days are the hard part

Years are at least a fixed concept. Months are not: February has 28 or 29 days, and the others alternate 30/31. So "one month later" has no single meaning, and "how many months between two dates" has three defensible answers.

The most common convention — and the one used by date libraries and most government forms — is the calendar-month convention: count whole calendar months, then count the remaining days against the day-of-month of the start date. A practical implementation:

function diff(start, end) {
  let years = end.getFullYear() - start.getFullYear();
  let months = end.getMonth() - start.getMonth();
  let days = end.getDate() - start.getDate();

  if (days < 0) {
    // borrow a month: step back to the last day of the previous month
    const prevMonth = new Date(end.getFullYear(), end.getMonth(), 0);
    days += prevMonth.getDate();
    months -= 1;
  }
  if (months < 0) {
    months += 12;
    years -= 1;
  }
  return { years, months, days };
}
Enter fullscreen mode Exit fullscreen mode

The new Date(y, m, 0) trick is the part people miss. Day 0 of a month is the last day of the previous month, which is exactly the borrow you need.

Worked example

Born 1990-08-15, today 2026-11-14.

  • Years: 2026 - 1990 = 36
  • Months: 10 - 7 = 3 (November is index 10, August is index 7)
  • Days: 14 - 15 = -1 → borrow: previous month is October, new Date(2026, 10, 0) = Oct 31, so days = 31 - 1 = 30, months = 2

Result: 36 years, 2 months, 30 days. Sanity check: 36 years and 3 months would be 2026-11-15, so one day earlier is 36/2/30. Correct.

Edge cases that actually bite

Leap day birthdays. Someone born 2000-02-29. In non-leap years, is their birthday Feb 28 or Mar 1? Different jurisdictions answer differently. Pick one, document it, and apply it consistently — do not let the borrow logic silently produce Feb 29 in a non-leap year.

Month-end borrowing across a short month. Start 2026-01-31, end 2026-03-01. Borrowing from February gives 28 days, so the result is 1 month and 1 day. But a user may expect "1 month and 1 day" only if they accept that January 31 + 1 month = February 28. State the convention.

Day-of-month overflow. Start 2026-01-30, add one month: new Date(2026, 1, 30) rolls over to March 2, not February 28. If you are adding months rather than diffing, clamp to the last valid day of the target month or you will drift.

Inclusive vs exclusive end dates. Age at a date is exclusive of that date's anniversary in some legal contexts and inclusive in others. Age difference between two birthdays is a different question again. Label which one your function answers.

Time zones. new Date('2000-08-15') is parsed as UTC midnight, while new Date(2000, 7, 15) is local midnight. Mixing the two constructors for the same birthday shifts the result by a day at month boundaries. Parse both inputs the same way.

Negative intervals. If end < start, the borrow logic above produces negative components in the wrong places. Either reject the input or implement a symmetric swap-and-negate.

Common mistakes

  • Using Math.floor((end - start) / 365.25). This is off by a day or two over long spans and fails entirely across leap years. Never estimate age from a day count.
  • Treating a month as 30 days. 30-day months and calendar months disagree about once every two months.
  • Ignoring the order of operations. Adjust days before months, or the borrow will be applied to the wrong year.
  • Assuming getMonth() is 1-based. It is not, and every off-by-one in the example above comes from that.
  • Rounding months up. If the anniversary is one day away, the correct answer is the smaller number of months plus days, not the next month.

Checking your own numbers

If you want to see the year/month/day decomposition laid out for any pair of dates, the age calculator at https://www.miryori.org/tools/age-calculator prints the breakdown alongside the total days, so you can compare the calendar-month convention against a raw day count.

Related reading

The same calculation, written up in more depth elsewhere:

YMYL note: date arithmetic has legal and financial consequences (age limits, eligibility, interest accrual). Verify against the specific rule that applies to your case, and test your implementation against known dates before shipping it.

Top comments (1)

Collapse
 
packetexchange_api profile image
Packet Exchange Api •

The time zone section is the one I would flag hardest. The UTC versus local midnight split you describe goes away if a birthday is never treated as an instant at all, just three integers for year, month and day, with Date used only for the day 0 borrow lookup.

The remaining trap is "today", which depends on whose zone you ask in.

Do you take the end date from the client or compute it on the server?