How to Calculate Age in JavaScript (DOB to Years, Months, Days)
Learn practical ways to calculate age from a date of birth in JavaScript. Includes exact years, months, days, an as-of date option, and common pitfalls like time zones and leap years.
If you need to calculate a person's age from a date of birth (DOB) in JavaScript, you can do it in a few different ways. The key is deciding what you mean by age:
- Age in completed years (most common)
- Exact age (years, months, days)
- Age as of a specific date (not always today)
1) Calculate age in completed years (simple and correct)
This is the standard approach: subtract years and adjust if the birthday hasn't happened yet this year.
function ageInYears(dob, asOf = new Date()) {
const birth = new Date(dob);
const ref = new Date(asOf);
let years = ref.getFullYear() - birth.getFullYear();
const m = ref.getMonth() - birth.getMonth();
if (m < 0 || (m === 0 && ref.getDate() < birth.getDate())) {
years--;
}
return years;
}
Example: DOB 2000-02-10 and as-of 2026-02-09 gives 25 (birthday tomorrow).
2) Calculate exact age (years, months, days)
If you want an exact breakdown, you can calculate years first, then months, then days by "borrowing" from the previous month when needed.
function exactAge(dob, asOf = new Date()) {
const birth = new Date(dob);
const ref = new Date(asOf);
let years = ref.getFullYear() - birth.getFullYear();
let months = ref.getMonth() - birth.getMonth();
let days = ref.getDate() - birth.getDate();
// Adjust days by borrowing from the previous month
if (days < 0) {
const prevMonth = new Date(ref.getFullYear(), ref.getMonth(), 0); // day 0 = last day of previous month
days += prevMonth.getDate();
months--;
}
// Adjust months by borrowing from the previous year
if (months < 0) {
months += 12;
years--;
}
return { years, months, days };
}
Note: Date-of-month edge cases (like a DOB on the 31st) require careful testing. If you need something robust across all locales and edge cases, consider using a well-tested date library.
3) Use an "as-of" date for age at a past or future date
Many apps need age at a specific reference date (insurance, eligibility rules, school year cutoffs). Both functions above accept an asOf parameter, so you can do:
ageInYears('1995-06-20', '2030-01-01');
exactAge('1995-06-20', '2030-01-01');
Common pitfalls (and how to avoid them)
- Time zones: parsing
YYYY-MM-DDcan behave differently across environments. If you only care about the date, build the Date in local time:new Date(year, monthIndex, day). - Leap day birthdays: decide your rule for Feb 29 on non-leap years (Feb 28 or Mar 1). Be consistent.
- Invalid inputs: validate that the DOB is a real date and not in the future.
Quick option: use the online age calculator
If you just need the answer (or you want to validate your implementation), use our free calculator: Calculate Age Online.
Related guides
- How to calculate age in Excel
- How to calculate age in Google Sheets
- Browse all age calculation guides
FAQ
What is the most accurate way to calculate age?
For most cases, "completed years" (age in whole years) is the standard. If you need a full breakdown, calculate years first, then months, then days, and test edge cases like month length and leap years.
Why does JavaScript sometimes give the wrong day for a date string?
Because new Date('YYYY-MM-DD') can be interpreted as UTC in some environments. If you care about the local calendar date, parse the parts and use new Date(y, m, d).