-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
34 lines (32 loc) · 1.15 KB
/
Copy pathscript.js
File metadata and controls
34 lines (32 loc) · 1.15 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
32
33
const birthDate = document.getElementById('birthdate');
const calculateBtn = document.getElementById('submit');
const age = document.getElementById('age');
const months = document.getElementById('months');
const days = document.getElementById('days');
calculateBtn.addEventListener('click', () => {
if(birthDate.value === '') {
alert('Please enter your birth date');
return;
}
const birthDateValue = new Date(birthDate.value);
const todayDate = new Date();
if(birthDateValue > todayDate) {
alert('Birth date cannot be in the future!');
return;
}
let years = todayDate.getFullYear() - birthDateValue.getFullYear();
let monthsDiff = todayDate.getMonth() - birthDateValue.getMonth();
let daysDiff = todayDate.getDate() - birthDateValue.getDate();
if(monthsDiff < 0 || (monthsDiff === 0 && daysDiff < 0)) {
years--;
monthsDiff += 12;
}
if(daysDiff < 0) {
const lastMonth = new Date(todayDate.getFullYear(), todayDate.getMonth(), 0);
daysDiff += lastMonth.getDate();
monthsDiff--;
}
age.textContent = `Age: ${years} years`;
months.textContent = `Months: ${monthsDiff}`;
days.textContent = `Days: ${daysDiff}`;
})