Calculate Age
Calculate age from birthdate
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));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
Detailed Age
Age in Units
Next Birthday
Use Cases
Best Practices
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.
Practical Learning Notes for Calculate Age
This JavaScript program is part of the "Date/Time Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.