-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
39 lines (25 loc) · 981 Bytes
/
script.js
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
37
38
39
let button = document.querySelector("button");
let showAge = document.getElementById("show_age");
let dateOfBirth = document.querySelector("input");
button.addEventListener("click",(e)=>{
e.preventDefault();
const today = new Date();
const dob = new Date(dateOfBirth.value)
let age = today.getFullYear() - dob.getFullYear();
const month = today.getMonth() - dob.getMonth();
const date = today.getDate() - dob.getDate();
if(dateOfBirth.value =="")
alert("Please enter your birthday");
else {
if(today.getMonth() < dob.getMonth() || today.getDate() < dob.getDate()){
age--;
showAge.innerText = `Your age is ${age} years old`
}
else {
if(today.getMonth() > dob.getMonth() || today.getDate() > dob.getDate()){
age--;
}
showAge.innerText = `Your age is ${-age} years old`
}
}
})