Calculate Age
Calculate age from birthdate
BeginnerTopic: Date/Time Programs
JavaScript Calculate Age Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Method 1: Simple age calculation
function calculateAge(birthDate) {
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
const birthDate = new Date('1990-05-15');
console.log('Age:', calculateAge(birthDate));
// Method 2: Age with months and days
function calculateAgeDetailed(birthDate) {
const today = new Date();
let years = today.getFullYear() - birthDate.getFullYear();
let months = today.getMonth() - birthDate.getMonth();
let days = today.getDate() - birthDate.getDate();
if (days < 0) {
months--;
const lastMonth = new Date(today.getFullYear(), today.getMonth(), 0);
days += lastMonth.getDate();
}
if (months < 0) {
years--;
months += 12;
}
return { years, months, days };
}
const ageDetailed = calculateAgeDetailed(birthDate);
console.log(`${ageDetailed.years} years, ${ageDetailed.months} months, ${ageDetailed.days} days`);
// Method 3: Age in different units
function ageInUnits(birthDate) {
const today = new Date();
const diff = today - birthDate;
return {
milliseconds: diff,
seconds: Math.floor(diff / 1000),
minutes: Math.floor(diff / (1000 * 60)),
hours: Math.floor(diff / (1000 * 60 * 60)),
days: Math.floor(diff / (1000 * 60 * 60 * 24)),
weeks: Math.floor(diff / (1000 * 60 * 60 * 24 * 7)),
months: Math.floor(diff / (1000 * 60 * 60 * 24 * 30.44)),
years: Math.floor(diff / (1000 * 60 * 60 * 24 * 365.25))
};
}
const ageUnits = ageInUnits(birthDate);
console.log('Age in units:', ageUnits);
// Method 4: Next birthday
function getNextBirthday(birthDate) {
const today = new Date();
const currentYear = today.getFullYear();
let nextBirthday = new Date(currentYear, birthDate.getMonth(), birthDate.getDate());
if (nextBirthday < today) {
nextBirthday.setFullYear(currentYear + 1);
}
return nextBirthday;
}
const nextBirthday = getNextBirthday(birthDate);
console.log('Next birthday:', nextBirthday);
// Method 5: Days until birthday
function daysUntilBirthday(birthDate) {
const nextBirthday = getNextBirthday(birthDate);
const today = new Date();
const diff = nextBirthday - today;
return Math.ceil(diff / (1000 * 60 * 60 * 24));
}
console.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: 121Understanding Calculate Age
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
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.