Calculate Age from Birthdate
Calculates accurate age from birthdate considering month and day
Implementation
Copy the complete example or adapt its typed interface to your project. Comments are kept inside the snippet so the intent travels with the code.
/**
* Calculate age from birthdate
* @param birthdate Birthdate (Date object or ISO string)
* @returns Calculated age in years
*/
function calculateAge(birthdate: Date | string): number {
const birth = new Date(birthdate);
const today = new Date();
let age = today.getFullYear() - birth.getFullYear();
const monthDiff = today.getMonth() - birth.getMonth();
// Adjust age if birthday hasn't occurred yet this year
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
age--;
}
return age;
}
// Usage Example
console.log(calculateAge('1990-05-20')); // 34 (if current year is 2024)
console.log(calculateAge(new Date(1990, 4, 20))); // 34When to use it
Useful for user profile pages, age verification, or any application requiring age calculation from birthdate
Review the input assumptions and browser or runtime support before adopting a utility unchanged. Small helpers are easiest to maintain when their contract stays narrow.
Reasoning and trade-offs
How it works
The calculation subtracts birth year from the current year, then checks whether the birthday has occurred in the current calendar year. If not, it subtracts one to avoid overstating the age.
What to watch for
Date parsing and time zones can shift a calendar date, especially when a date-only string becomes UTC midnight. Leap-day birthdays and legal age rules can differ by jurisdiction and should not be inferred from a generic utility.
Production use
Use local calendar components from an explicitly parsed date-only value for ordinary display. For eligibility, compliance, or healthcare decisions, encode the jurisdictional rule and test boundary dates with domain experts.
Before you ship
- Confirm the input contract with tests that cover empty, invalid, boundary, and representative real-world values before sharing the helper across a codebase.
- Keep the function focused on one responsibility and name any project-specific policy explicitly instead of hiding it inside a generic-looking utility.
- Measure behavior in the browsers or runtime versions your product supports, especially when timing, storage, DOM state, locale, or network access is involved.
- Document the return type, mutation behavior, failure mode, and performance characteristics beside the call site so future maintainers can evaluate changes safely.
- Compare the example with current official platform documentation when it relies on a browser API, language feature, or runtime behavior that may change over time.
- If the helper crosses a trust boundary, validate again on the server and avoid treating browser-side checks, TypeScript annotations, or formatted output as a security guarantee.
- Add a small regression test whenever a real edge case is discovered; concrete examples preserve the reason behind a guard more reliably than a general comment.