Calculate Age

Calculate age in years from a birth date, accounting for month and day.

Code

General
const today = new Date();
const birth = new Date(birthDate);
let age = today.getFullYear() - birth.getFullYear();
const m = today.getMonth() - birth.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birth.getDate())) age--;
return age;

Parameters

Birth date (YYYY-MM-DD)

Browser·fetch() may be limited by CORS

More JavaScript Snippets