summaryrefslogtreecommitdiff
path: root/assets/js/main.js
blob: bd14b0c9cc84f5699b1df91583dcc0ac88bc5f14 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const elements = document.querySelectorAll(".my-age")

const specificDate = new Date("2010-05-11T22:50:00");
const updateAge = () => {
    const currentDate = new Date();
    const age = (
        (currentDate - specificDate) /
        (1000 * 60 * 60 * 24 * 365.25)
    ).toFixed(15);
    let nextBirthday = new Date(specificDate);
    nextBirthday.setFullYear(currentDate.getFullYear());
    if (currentDate > nextBirthday) {
        nextBirthday.setFullYear(currentDate.getFullYear() + 1);
    }
    const timeDiff = nextBirthday - currentDate;
    const daysLeft = Math.floor(timeDiff / (1000 * 60 * 60 * 24));

    elements.forEach(e => {
        e.textContent = `${age}`;
        e.title = `${daysLeft} days left`;
    })
    requestAnimationFrame(updateAge);
}
updateAge();