-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
62 lines (52 loc) · 1.71 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
<!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_Calc</title>
<link rel="stylesheet" href="AgeCalc.css">
</head>
<body>
<div class="wrapper">
<div class="age-box">
<h1>Age<span>Calc</span></h1>
<div class="input-container">
<input type="date" id="dobInput" />
<button onclick="displayAge()">Calculate Age</button>
</div>
<p id="outputResult"></p>
</div>
</div>
<!-- main part -->
<script>
const dobInput = document.getElementById("dobInput");
dobInput.max = new Date().toISOString().split("T")[0];
const output = document.getElementById("outputResult");
function displayAge() {
const birthDate = new Date(dobInput.value);
if (isNaN(birthDate)) {
output.innerHTML = "<span>Please enter a valid date.</span>";
return;
}
const today = new Date();
let years = today.getFullYear() - birthDate.getFullYear();
let months = today.getMonth() - birthDate.getMonth();
let days = today.getDate() - birthDate.getDate();
if (days < 0) {
months--;
days += getDaysInMonth(today.getFullYear(), today.getMonth());
}
if (months < 0) {
years--;
months += 12;
}
output.innerHTML =
`<strong>${years}</strong> years, <strong>${months}</strong> months, and <strong>${days}</strong> days old.`;
}
function getDaysInMonth(year, month) {
return new Date(year, month + 1, 0).getDate();
}
</script>
</body>
</html>