-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsite-scripts.html
More file actions
36 lines (31 loc) · 1005 Bytes
/
Copy pathsite-scripts.html
File metadata and controls
36 lines (31 loc) · 1005 Bytes
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
26
27
28
29
30
31
32
33
34
35
36
<script>
(function () {
function parseLocalDate(value) {
var parts = value.split("-").map(Number);
return new Date(parts[0], parts[1] - 1, parts[2]);
}
function calculateAge(birthDate, today) {
var age = today.getFullYear() - birthDate.getFullYear();
var birthdayThisYear = new Date(
today.getFullYear(),
birthDate.getMonth(),
birthDate.getDate()
);
if (today < birthdayThisYear) age -= 1;
return age;
}
document.querySelectorAll("[data-birthdate]").forEach(function (element) {
var birthDate = parseLocalDate(element.dataset.birthdate);
if (Number.isNaN(birthDate.getTime())) return;
var age = calculateAge(birthDate, new Date());
var format = element.dataset.ageFormat || "years-old";
if (format === "adjective") {
element.textContent = age + "-year-old";
} else if (format === "number") {
element.textContent = age;
} else {
element.textContent = age + " years old";
}
});
}());
</script>