Skip to content

Commit 87562a3

Browse files
committed
age calculator
1 parent 616e7b6 commit 87562a3

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed
80.4 KB
Loading

14. Age Calculator/index.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Age Calculator</title>
8+
<link rel="stylesheet" href="style.css">
9+
<script defer src="script.js"></script>
10+
</head>
11+
12+
<body>
13+
<div class="container">
14+
<div class="content">
15+
<h1>Javascript</h1>
16+
<h1>Age Calculator</h1>
17+
<input type="date" id="date">
18+
<input type="button" value="Calculate" onclick="calculateAge()" id="calculateAge">
19+
<div>
20+
<p id="display">Select your date of birth.</p>
21+
</div>
22+
</div>
23+
24+
25+
</div>
26+
27+
</body>
28+
29+
</html>

14. Age Calculator/script.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
let userInput = document.getElementById("date");
2+
let display = document.getElementById("display");
3+
4+
userInput.max = new Date().toISOString().split("T")[0]; // disable future date
5+
6+
function calculateAge() {
7+
let birthDate = new Date(userInput.value);
8+
9+
let userDate = birthDate.getDate();
10+
let userMonth = birthDate.getMonth() + 1;
11+
let userYear = birthDate.getFullYear();
12+
13+
14+
let today = new Date();
15+
16+
17+
let todaysDate = today.getDate();
18+
let todaysMonth = today.getMonth() + 1;
19+
let todaysYear = today.getFullYear();
20+
21+
22+
let date3, month3, year3;
23+
24+
year3 = todaysYear - userYear;
25+
26+
if (todaysMonth >= userMonth) {
27+
month3 = todaysMonth - userMonth;
28+
}
29+
else {
30+
month3--;
31+
month3 = 12 + todaysMonth - userMonth;
32+
}
33+
if (todaysDate >= userDate) {
34+
date3 = todaysDate - userDate;
35+
}
36+
else {
37+
month3--;
38+
date3 = getDateInMonth(userYear, userMonth) + todaysDate - userDate;
39+
}
40+
if (month3 < 0) {
41+
month3 == 11;
42+
year3--
43+
}
44+
// console.log(year3,month3,date3);
45+
// display.innerHTML = `Your Age is ${year3} years, ${month3} months and ${date3} days`;
46+
display.innerText = `Your Age is ${year3} years, ${month3} months and ${date3} days`;
47+
48+
}
49+
50+
function getDateInMonth(year, month) {
51+
return new Date(year, month, 0).getDate();
52+
}
53+

14. Age Calculator/style.css

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
box-sizing: border-box;
5+
}
6+
7+
.content h1:nth-child(1) {
8+
color: rgba(27, 6, 122, 0.604);
9+
}
10+
11+
.content h1:nth-child(2) {
12+
color: rgb(27, 6, 122);
13+
}
14+
15+
#display {
16+
color: rgba(33, 57, 87, 0.784);
17+
font-size: 18px;
18+
padding-left: 5px;
19+
20+
}
21+
22+
.container {
23+
background: linear-gradient(#3731e6e2, #0834adab);
24+
height: 100vh;
25+
width: 100vw;
26+
display: flex;
27+
align-items: center;
28+
justify-content: center;
29+
flex-direction: column;
30+
font-family: 'Poppins', sans-serif;
31+
color: white;
32+
font-size: 1.5rem;
33+
}
34+
35+
.content {
36+
position: absolute;
37+
left: 50%;
38+
top: 50%;
39+
transform: translate(-50%, -50%);
40+
}
41+
42+
input {
43+
width: 200px;
44+
padding: 10px;
45+
border: 1px solid #ccc;
46+
outline: none;
47+
border-radius: 5px;
48+
font-size: 1rem;
49+
cursor: pointer;
50+
margin-top: 10px;
51+
margin-bottom: 10px;
52+
}
53+
54+
#calculateAge {
55+
width: fit-content;
56+
padding-left: 15px;
57+
padding-right: 15px;
58+
border-color: transparent;
59+
background-color: rgba(255, 255, 255, 0.805);
60+
61+
62+
}
63+
64+
#date {
65+
background-color: rgba(255, 255, 255, 0.805);
66+
border-color: transparent;
67+
}

0 commit comments

Comments
 (0)