-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
31 lines (27 loc) · 1.03 KB
/
script.js
File metadata and controls
31 lines (27 loc) · 1.03 KB
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
const calculateButton = document.getElementById('calculate');
const dobInput = document.getElementById('dob');
const resultDiv = document.getElementById('result');
flatpickr(dobInput, {
dateFormat: 'Y-m-d',
maxDate: new Date(),
onChange: () => {
dobInput.value = moment(dobInput.value).format('YYYY-MM-DD');
}
});
function calculateAge() {
const dob = moment(dobInput.value);
const now = moment();
const minutes = now.diff(dob, 'minutes');
const hours = now.diff(dob, 'hours');
const days = now.diff(dob, 'days');
const months = now.diff(dob, 'months');
resultDiv.innerHTML = `
<div class="result">
<p>You lived <span class="or">${minutes.toLocaleString()}</span> minutes</p>
<p>You lived <span class="or">${hours.toLocaleString()}</span> hours</p>
<p>You lived <span class="or">${days.toLocaleString()}</span> days</p>
<p>You lived <span class="or">${months.toLocaleString()}</span> months</p>
</div>
`;
}
calculateButton.addEventListener('click', calculateAge);