my-website/static/scripts/calcage.js

19 lines
587 B
JavaScript

"use strict";
{
// Month index must be two because counting begins at 0 (for January)
const myBirthDate = new Date(2001, 2, 29);
const today = new Date();
let yearsSinceBirth = today.getFullYear() - myBirthDate.getFullYear();
// Correct yearsSinceBirth if between New Year and my birthday
if (today.getMonth() < myBirthDate.getMonth()) yearsSinceBirth--;
else if (
today.getMonth() == myBirthDate.getMonth() &&
today.getDate() < myBirthDate.getDate()
)
yearsSinceBirth--;
document.getElementById("age-display").innerText = yearsSinceBirth.toString();
}