UNPKG

age-calculator-tepllo

Version:

Simple age calculator from birth date

18 lines (14 loc) 424 B
function calculateAge(birthDate) { const today = new Date(); const birth = new Date(birthDate); if (isNaN(birth)) { throw new Error("Invalid date format. Use YYYY-MM-DD"); } let age = today.getFullYear() - birth.getFullYear(); const m = today.getMonth() - birth.getMonth(); if (m < 0 || (m === 0 && today.getDate() < birth.getDate())) { age--; } return age; } module.exports = { calculateAge };