Calculate Age in JavaScript
Calculate age from birthdate
BeginnerDate/Time ProgramsExample 8 of 10
calculate-age.js
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));
Output
Age: 33
33 years, 8 months, 0 days
Age in units: { milliseconds: 1062720000000, seconds: 1062720000, minutes: 17712000, hours: 295200, days: 12300, weeks: 1757, months: 404, years: 33 }
Next birthday: 2024-05-15T00:00:00.000Z
Days until birthday: 121What's going on
Age calculation determines time since birth.
Basic Calculation
Subtract years
Adjust for month/day
Handle birthday not yet passed
Detailed Age
Years, months, days
Handle month boundaries
Accurate calculation
Age in Units
Various time units
Milliseconds to years
Useful for display
Next Birthday
Calculate next occurrence
Handle year rollover
Days until birthday
Use Cases
User profiles
Age verification
Birthday reminders
Statistics
Best Practices
Handle edge cases
Consider timezones
Format nicely
Validate input