-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
78 lines (61 loc) · 1.91 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Age Wizard - An Age Calculator</title>
<link rel="stylesheet" href="style.css">
<link rel="icon" type="image/x-icon" href="/assets/AgeWizard.jpg">
<meta name="google-site-verification" content="iNzSj2wvdEOVu1PtrK5FfEiu_teOG2LQn5i3kg-xozQ" />
</head>
<body>
<div class="container">
<div class="calculator">
<h1><span> Age Wizard </span></h1>
<div class="input-box">
<input type="date" id="date">
<button onclick="calculateAge()">Calculate Age</button>
</div>
<p id="result"></p>
</div>
</div>
<script>
let userInput = document.getElementById("date")
userInput.max = new Date().toISOString().split("T")[0];
let result = document.getElementById("result");
function calculateAge(){
let birthDate = new Date(userInput.value);
let d1 = birthDate.getDate();
let m1 = birthDate.getMonth() + 1;
let y1 = birthDate.getFullYear();
let today = new Date();
let d2 = today.getDate();
let m2 = today.getMonth() + 1;
let y2 = today.getFullYear();
let d3,m3,y3;
y3 = y2 - y1;
if(m2 >= m1){
m3 = m2 - m1;
}else{
y3--;
m3 = 12 + m2 - m1;
}
if(d2 >= d1){
d3 = d2 - d1;
}else{
m3--;
d3 = getDaysInMonth(y1, m1) + d2 - d1;
}
if(m3<0){
m3 = 11;
y3--;
}
result.innerHTML=`You are <span>${y3}</span> Years, <span>${m3}</span> Months, <span>${d3}</span> Days Old`;
}
function getDaysInMonth(year, month){
return new Date(year, month, 0).getDate();
}
</script>
</body>
</html>