-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathageCalculator.html
More file actions
116 lines (106 loc) · 3.03 KB
/
ageCalculator.html
File metadata and controls
116 lines (106 loc) · 3.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>age Calculator</title>
<style>
.container{
height:400px;
font-family: 'Courier New', Courier, monospace;
width:600px;
margin: 6rem auto;
background-color: #5c040434;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
border-radius: 15px;
border: 2px solid #5c040434 ;
}
h1{
text-align: center;
font-weight: 700;
font-size: 45px;
margin: 20px;
}
h3{
text-align: center;
font-weight: 500;
font-size:25px;
margin: 10px;
}
#date{
border: none;
outline: none;
padding: 20px;
margin: 25px auto;
width:80%;
font-size: 20px;
display: flex;
align-items: center;
justify-content: center;
border-radius: 10px;
}
#btn{
font-size: 22px;
background-color: #1c1ccaa2;
color: white;
padding: 20px;
font-weight: 400px;
letter-spacing: 3px;
margin: 20px auto;
border: 2px solid #0000ff8e;
border-radius: 3px;
display: flex;
align-items: center;
justify-content: center;
}
button:hover{
opacity: 0.5;
}
#final{
font-weight: 600;
text-align: center;
margin:20px;
}
</style>
</head>
<body>
<div class="container">
<h1>AGE CALCULATOR</h1>
<h3>Enter you date of birth</h3>
<label for="date">
<input type="date" placeholder="dd//mm//yyyy" id="date">
</label>
<button id="btn">Calculate</button>
<h2 id="final">Your age is 21 years old</h2>
</div>
<script>
const dateE1 = document.getElementById("date");
const buttonE1 = document.getElementById("btn");
const finalE1 = document.getElementById("final");
function calculateAge(){
const birthdayVal = dateE1.value;
if(birthdayVal === ""){
alert("Please enter your birthday");
}else{
const age = getValue(birthdayVal);
finalE1.innerText = `Your Age is ${age} ${age > 1 ? "years" : "year"} old`;
console.log(age);
}
}
function getValue(birthdayVal){
const currentDate = new Date();
const birthdayDate = new Date(birthdayVal);
let age = currentDate.getFullYear() - birthdayDate.getFullYear();
const month = currentDate.getMonth() - birthdayDate.getMonth();
if (
month < 0 ||
(month === 0 && currentDate.getDate() < birthdayDate.getDate())
) {
age--;
}
return age;
}
buttonE1.addEventListener("click", calculateAge);
</script>
</body>
</html>