function calculateAge(birthDate)
Calculates age in years from a given birth date
Name↑ | Type | Required | Description |
|---|---|---|---|
| birthDate | date | Required | The birth date to calculate age from |
function calculateAge(birthDate) {
const today = new Date();
const birth = new Date(birthDate);
let age = today.getFullYear() - birth.getFullYear();
const monthDiff = today.getMonth() - birth.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
age--;
}
return age;
}