Calculate Age in JavaScript
Calculate age from a birthdate in JavaScript: whole years, years-months-days, age in other units, next birthday and days left until it. Explanation and output.
The sample output was captured with the clock set to 15 January 2024, 10:30:45 UTC. Your run will show your own date, time and time zone.
1// Method 1: Simple age calculation2function calculateAge(birthDate) {3 const today = new Date();4 let age = today.getFullYear() - birthDate.getFullYear();5 const monthDiff = today.getMonth() - birthDate.getMonth();67 if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {8 age--;9 }1011 return age;12}1314const birthDate = new Date('1990-05-15');15console.log('Age:', calculateAge(birthDate));1617// Method 2: Age with months and days18function calculateAgeDetailed(birthDate) {19 const today = new Date();20 let years = today.getFullYear() - birthDate.getFullYear();21 let months = today.getMonth() - birthDate.getMonth();22 let days = today.getDate() - birthDate.getDate();2324 if (days < 0) {25 months--;26 const lastMonth = new Date(today.getFullYear(), today.getMonth(), 0);27 days += lastMonth.getDate();28 }2930 if (months < 0) {31 years--;32 months += 12;33 }3435 return { years, months, days };36}3738const ageDetailed = calculateAgeDetailed(birthDate);39console.log(`${ageDetailed.years} years, ${ageDetailed.months} months, ${ageDetailed.days} days`);4041// Method 3: Age in different units42function ageInUnits(birthDate) {43 const today = new Date();44 const diff = today - birthDate;4546 return {47 milliseconds: diff,48 seconds: Math.floor(diff / 1000),49 minutes: Math.floor(diff / (1000 * 60)),50 hours: Math.floor(diff / (1000 * 60 * 60)),51 days: Math.floor(diff / (1000 * 60 * 60 * 24)),52 weeks: Math.floor(diff / (1000 * 60 * 60 * 24 * 7)),53 months: Math.floor(diff / (1000 * 60 * 60 * 24 * 30.44)),54 years: Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25))55 };56}5758const ageUnits = ageInUnits(birthDate);59console.log('Age in units:', ageUnits);6061// Method 4: Next birthday62function getNextBirthday(birthDate) {63 const today = new Date();64 const currentYear = today.getFullYear();65 let nextBirthday = new Date(currentYear, birthDate.getMonth(), birthDate.getDate());6667 if (nextBirthday < today) {68 nextBirthday.setFullYear(currentYear + 1);69 }7071 return nextBirthday;72}7374const nextBirthday = getNextBirthday(birthDate);75console.log('Next birthday:', nextBirthday);7677// Method 5: Days until birthday78function daysUntilBirthday(birthDate) {79 const nextBirthday = getNextBirthday(birthDate);80 const today = new Date();81 const diff = nextBirthday - today;82 return Math.ceil(diff / (1000 * 60 * 60 * 24));83}8485console.log('Days until birthday:', daysUntilBirthday(birthDate));
Sample Output
Age in units: {...}Counted from 15 May 1990 at midnight UTC to 15 January 2024, 10:30:45.
Days until birthday: 121
From 15 January at 10:30 to 15 May at midnight is about 120.6 days. Math.ceil rounds it up to 121.
In this one
- How to adjust the year difference when the birthday has not passed
- How to borrow months and days for a detailed age
- Why unit conversions are approximate
- How to find the next birthday and days until it
How it works
- 1
calculateAge(1990-05-15) gives 33.
- 2
calculateAgeDetailed gives 33 years, 8 months, 0 days.
- 3
ageInUnits converts the gap to seconds, days, weeks and more.
- 4
getNextBirthday returns 15 May 2024.
- 5
daysUntilBirthday returns 121.
Edge cases
Born on 29 February
In a normal year new Date(year, 1, 29) becomes 1 March.
Decide whether to celebrate on 28 February or 1 March.
Birthday today
getNextBirthday may return today at midnight, which is already past by morning.
It then moves to next year. Compare dates without times if today should count.
Future birth date
Age becomes negative.
Validate the input first.
What's going on
Age calculation shows up in admission forms, profile pages and age checks. It looks easy, just subtract years, but birthdays make it tricky.
Method 1 subtracts the birth year from this year. Then it checks if the birthday has happened yet this year. If the current month is before the birth month, or it is the birth month but an earlier day, the birthday has not come yet, so it subtracts 1. On 15 January 2024, someone born on 15 May 1990 is 33, not 34.
Method 2 gives years, months and days. It subtracts each part. If days go negative, it borrows a month and adds the number of days in the previous month. new Date(year, month, 0) is a trick that gives the last day of the previous month. If months go negative, it borrows a year and adds 12. The result here is 33 years, 8 months, 0 days.
Method 3 converts the total milliseconds into other units. Months use an average of 30.44 days and years use 365.25 days, because real months and years vary. These are approximate, good for fun facts but not for legal age.
Method 4 finds the next birthday. It builds this year's birthday. If that date is already past, it moves it to next year.
Method 5 counts days until that birthday. Math.ceil rounds up, so any part of a day counts as a day. 15 January to 15 May 2024 is 121 days, because 2024 is a leap year.
Edge cases need thought. Someone born on 29 February has no birthday in normal years. Time zones can shift the day. Validate that the birth date is real and not in the future.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
On 1 March 2024, how old is someone born 15 May 2000?
Pick an answer to check it.
Question 2
Which rounding does daysUntilBirthday use?
Pick an answer to check it.
Questions people ask
Why not divide total days by 365?
Leap years add extra days, so the answer can be off near a birthday.
What does new Date(2024, 0, 0) return?
31 December 2023, the last day of the previous month.
Why are the units approximate?
Months and years have different lengths, so averages are used.