If you need to calculate age once, a calculator is faster. But when you have a spreadsheet of 5,000 birth dates, or you are building a signup form that checks whether a user is 18, you need a formula or a few lines of code. The good news: every common tool can do it. The catch: the naive version is almost always subtly wrong.
Below are tested, copy-paste solutions for Excel, Google Sheets, Python, and JavaScript — each one correctly handling the case that breaks most attempts: when the birthday has not yet happened this year.
The One Rule Everything Hinges On
Age is the number of completed years since birth. So:
age = thisYear − birthYear
if (birthday has not occurred yet this year) age = age − 1
"Has the birthday occurred" means: is today's (month, day) on or after the birth (month, day)? Every correct implementation below encodes exactly this check. The buggy ones forget it and report someone as a year older the day before their birthday.
Excel
Best: DATEDIF
Excel has a (semi-hidden) function built for this. With the birth date in A2:
=DATEDIF(A2, TODAY(), "Y")
DATEDIF returns completed periods, so the "Y" unit already handles the birthday check correctly. Other useful units:
=DATEDIF(A2, TODAY(), "M") ' total completed months
=DATEDIF(A2, TODAY(), "D") ' total days lived
For a full "X years, Y months, Z days" string:
=DATEDIF(A2,TODAY(),"Y")&" years, "&DATEDIF(A2,TODAY(),"YM")&" months, "&DATEDIF(A2,TODAY(),"MD")&" days"
"YM" is months ignoring years; "MD" is days ignoring months and years. (Note: DATEDIF does not appear in Excel's autocomplete — type it in full. It is a legacy function kept for compatibility.)
Alternative: YEARFRAC
For a decimal age:
=INT(YEARFRAC(A2, TODAY()))
INT floors it to completed years. Avoid (TODAY()-A2)/365 — dividing by 365 ignores leap years and drifts over time.
Google Sheets
Google Sheets supports the same DATEDIF and YEARFRAC formulas — they work identically.
Python
Python has no built-in age function, so you implement the rule directly. The cleanest version uses a boolean subtraction trick:
from datetime import date
def calculate_age(born: date, on: date | None = None) -> int:
on = on or date.today()
# (m, d) tuple comparison is True when birthday hasn't happened yet
had_birthday = (on.month, on.day) >= (born.month, born.day)
return on.year - born.year - (0 if had_birthday else 1)
print(calculate_age(date(1990, 4, 12))) # 36 on 2026-06-01
print(calculate_age(date(2000, 12, 25))) # 25 on 2026-06-01 (birthday not reached)
Comparing the (month, day) tuples is a neat, bug-resistant way to ask "have we passed the birthday this year." Subtract 1 when we have not.
For a full years/months/days breakdown, the third-party dateutil library is the standard choice:
from dateutil.relativedelta import relativedelta
from datetime import date
diff = relativedelta(date.today(), date(1990, 4, 12))
print(diff.years, diff.months, diff.days)
And for total days lived, plain subtraction works because date objects yield a timedelta:
days_lived = (date.today() - date(1990, 4, 12)).days
JavaScript
In the browser or Node, the Date object gets you there. Mind the gotcha: months are zero-indexed (January is 0, December is 11).
function calculateAge(birthDate, on = new Date()) {
let age = on.getFullYear() - birthDate.getFullYear();
const monthDiff = on.getMonth() - birthDate.getMonth();
// Subtract a year if the birthday hasn't occurred yet this year
if (monthDiff < 0 || (monthDiff === 0 && on.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
calculateAge(new Date(1990, 3, 12)); // month 3 = April → 36 on 2026-06-01
calculateAge(new Date(2000, 11, 25)); // month 11 = December → 25 (birthday not reached)
For total days lived, subtract the timestamps and divide by milliseconds per day:
const msPerDay = 1000 * 60 * 60 * 24;
const daysLived = Math.floor((Date.now() - birthDate.getTime()) / msPerDay);
Because UTC midnight vs. local time can shift a date by a few hours, normalize both dates to the same time (or use a date-only library like date-fns or Luxon) for production code where an off-by-one day is unacceptable.
Side-by-Side Cheat Sheet
| Task | Excel / Sheets | Python | JavaScript |
|---|---|---|---|
| Age in years | =DATEDIF(A2,TODAY(),"Y") |
relativedelta(today, dob).years |
function above |
| Total days | =DATEDIF(A2,TODAY(),"D") |
(today - dob).days |
timestamp ÷ msPerDay |
| Total months | =DATEDIF(A2,TODAY(),"M") |
rd.years*12 + rd.months |
derive from y/m |
| Pitfall | don't divide by 365 | none if using dateutil | months are 0-indexed |
When to Skip the Code
If you just need one answer — or want to sanity-check that your formula is correct — paste the date of birth into the age calculator and compare. It returns the exact age in years, months, and days plus total months, weeks, days, hours, and minutes, so it doubles as a quick test oracle for the code above.
For one-off lookups on a specific date there is the age calculator by date, and for measuring any duration the date difference calculator. For the full manual reasoning behind these formulas, see the companion guide on how to calculate age from a date of birth.
FAQ
What is the formula to calculate age in Excel?
Use =DATEDIF(A2, TODAY(), "Y"), where A2 holds the date of birth. The "Y" unit returns completed years and handles the birthday check automatically. The same formula works in Google Sheets. Avoid (TODAY()-A2)/365, which ignores leap years and drifts over time.
Why doesn't DATEDIF appear in Excel's function list?
DATEDIF is a legacy function kept for backward compatibility, so it is intentionally omitted from autocomplete and the function wizard. It still works perfectly — just type the full name and arguments yourself.
How do I calculate age in Python?
Subtract the birth year from the current year, then subtract one more if the birthday has not happened yet this year. A clean way is to compare (month, day) tuples. For a years/months/days breakdown, use relativedelta from the dateutil library.
What is the common bug when calculating age in JavaScript?
Two things: forgetting that Date months are zero-indexed (January is 0), and failing to subtract a year when the birthday has not yet occurred this year. Always compare the month and day, and normalize times to avoid timezone-driven off-by-one-day errors.
How do I get age in years, months, and days instead of just years?
In Excel, combine DATEDIF with the "Y", "YM", and "MD" units. In Python, use relativedelta and read .years, .months, and .days. In JavaScript, apply the same borrow-and-subtract logic across each unit or use a date library.
How can I verify my age formula is correct?
Paste the same date of birth into the age calculator and compare its output to your formula. Test the edge cases specifically — a birthday that is one day away, and a February 29 birth date — since those are where naive implementations fail.