Calculate Age in JavaScript

Calculate age from birthdate

BeginnerDate/Time ProgramsExample 8 of 10
calculate-age.js
1// Method 1: Simple age calculation
2function calculateAge(birthDate) {
3 const today = new Date();
4 let age = today.getFullYear() - birthDate.getFullYear();
5 const monthDiff = today.getMonth() - birthDate.getMonth();
6
7 if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
8 age--;
9 }
10
11 return age;
12}
13
14const birthDate = new Date('1990-05-15');
15console.log('Age:', calculateAge(birthDate));
16
17// Method 2: Age with months and days
18function 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();
23
24 if (days < 0) {
25 months--;
26 const lastMonth = new Date(today.getFullYear(), today.getMonth(), 0);
27 days += lastMonth.getDate();
28 }
29
30 if (months < 0) {
31 years--;
32 months += 12;
33 }
34
35 return { years, months, days };
36}
37
38const ageDetailed = calculateAgeDetailed(birthDate);
39console.log(`${ageDetailed.years} years, ${ageDetailed.months} months, ${ageDetailed.days} days`);
40
41// Method 3: Age in different units
42function ageInUnits(birthDate) {
43 const today = new Date();
44 const diff = today - birthDate;
45
46 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}
57
58const ageUnits = ageInUnits(birthDate);
59console.log('Age in units:', ageUnits);
60
61// Method 4: Next birthday
62function getNextBirthday(birthDate) {
63 const today = new Date();
64 const currentYear = today.getFullYear();
65 let nextBirthday = new Date(currentYear, birthDate.getMonth(), birthDate.getDate());
66
67 if (nextBirthday < today) {
68 nextBirthday.setFullYear(currentYear + 1);
69 }
70
71 return nextBirthday;
72}
73
74const nextBirthday = getNextBirthday(birthDate);
75console.log('Next birthday:', nextBirthday);
76
77// Method 5: Days until birthday
78function 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}
84
85console.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: 121

What'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