-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
47 lines (47 loc) · 2.24 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
<!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>
<link rel="stylesheet" href="styles.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Handjet:[email protected]&display=swap" rel="stylesheet">
<script>
function calculateAge() {
const birthDate = new Date(document.getElementById('birthdate').value);
const today = new Date();
const ageInMilliseconds = today - birthDate;
const ageInDays = Math.floor(ageInMilliseconds / (1000 * 60 * 60 * 24));
const ageInWeeks = Math.floor(ageInDays / 7);
const ageInMonths = (today.getFullYear() - birthDate.getFullYear()) * 12 + (today.getMonth() - birthDate.getMonth());
const ageInYears = today.getFullYear() - birthDate.getFullYear();
if (
today.getMonth() < birthDate.getMonth() ||
(today.getMonth() === birthDate.getMonth() && today.getDate() < birthDate.getDate())
) {
ageInYears--;
}
document.getElementById('years').innerText = ageInYears;
document.getElementById('months').innerText = ageInMonths;
document.getElementById('weeks').innerText = ageInWeeks;
document.getElementById('days').innerText = ageInDays;
}
</script>
</head>
<body>
<div class="block">
<h1 class="heading">Age Calculator <img src="heart.png" alt="heart" height="20"></h1>
<div class="content">
<input id="birthdate" class="input" type="date" required placeholder="Enter your DOB">
<button onclick="calculateAge()"><span class="font-issue">Submit</span></button>
<h2>Your Age in Years: <span id="years"></span></h2>
<h2>Your Age in Months: <span id="months"></span></h2>
<h2>Your Age in Weeks: <span id="weeks"></span></h2>
<h2>Your Age in Days: <span id="days"></span></h2><br>
@designed by Aluru Bala Karthikeya
</div>
</div>
</body>
</html>