my-website/static/scripts/calcage.js

20 lines
587 B
JavaScript
Raw Normal View History

"use strict";
{
2023-03-10 15:08:43 +01:00
// Month index must be two because counting begins at 0 (for January)
const myBirthDate = new Date(2001, 2, 29);
const today = new Date();
2023-03-10 15:08:43 +01:00
let yearsSinceBirth = today.getFullYear() - myBirthDate.getFullYear();
2023-03-10 15:08:43 +01:00
// 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--;
2023-03-10 15:08:43 +01:00
document.getElementById("age-display").innerText = yearsSinceBirth.toString();
}