Skip to content

Commit a0dc976

Browse files
committed
Age Calculator Application
1 parent 1bc967a commit a0dc976

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 Application</title>
8+
<link rel="stylesheet" href="style.css">
9+
</head>
10+
11+
<body>
12+
13+
<div class="container">
14+
<div class="calculator">
15+
<h1>JavaScript <br><span>Age Calculator</span></h1>
16+
<div class="input-box">
17+
<input type="date" id="dob">
18+
<button onclick="calculateAge()">Calculate</button>
19+
</div>
20+
<p id="result"></p>
21+
</div>
22+
</div>
23+
24+
<script src="script.js"></script>
25+
26+
</body>
27+
28+
</html>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
let userInput = document.getElementById("dob");
2+
userInput.max = new Date().toISOString().split("T")[0];
3+
let result = document.getElementById("result");
4+
5+
function calculateAge() {
6+
let birthDate = new Date(userInput.value);
7+
8+
let d1 = birthDate.getDate();
9+
let m1 = birthDate.getMonth() + 1;
10+
let y1 = birthDate.getFullYear();
11+
12+
let today = new Date();
13+
14+
let d2 = new Date().getDate();
15+
let m2 = new Date().getMonth() + 1;
16+
let y2 = new Date().getFullYear();
17+
18+
let d3, m3, y3;
19+
20+
y3 = y2 - y1;
21+
22+
if (m2 >= m1) {
23+
m3 = m2 - m1;
24+
} else {
25+
y3--;
26+
m3 = 12 + m2 - m1;
27+
}
28+
if (d2 >= d1) {
29+
d3 = d2 - d1;
30+
} else {
31+
m3--;
32+
d3 = getDaysInMonth(y1, m1) + d2 - d1;
33+
}
34+
if (m3 < 0) {
35+
m3 = 11;
36+
y3--;
37+
}
38+
result.innerHTML = `You are <span>${y3}</span> years <span>${m3}</span> months <span>${d3}</span> days old.`;
39+
}
40+
41+
function getDaysInMonth(year, month) {
42+
return new Date(year, month, 0).getDate();
43+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
font-family: sans-serif;
5+
box-sizing: border-box;
6+
}
7+
8+
.container {
9+
width: 100%;
10+
min-height: 100vh;
11+
background: linear-gradient(135deg, #4203a9, #90bafc);
12+
color: #fff;
13+
padding: 10px;
14+
}
15+
16+
.calculator {
17+
width: 100%;
18+
max-width: 600px;
19+
margin-left: 10%;
20+
margin-top: 10%;
21+
}
22+
23+
.calculator h1 {
24+
font-size: 3rem;
25+
}
26+
27+
.calculator h1 span {
28+
color: #ffff76;
29+
}
30+
31+
.input-box {
32+
margin: 40px 0;
33+
padding: 35px;
34+
border-radius: 10px;
35+
background: rgba(255, 255, 255, 0.3);
36+
display: flex;
37+
align-items: center;
38+
}
39+
40+
.input-box input {
41+
flex: 1;
42+
margin-right: 20px;
43+
padding: 14px 20px;
44+
border: 0;
45+
outline: 0;
46+
font-size: 18px;
47+
border-radius: 5px;
48+
position: relative;
49+
}
50+
51+
.input-box button {
52+
background: #ffff76;
53+
border: 0;
54+
outline: 0;
55+
padding: 15px 30px;
56+
border-radius: 5px;
57+
font-weight: 600;
58+
color: #333;
59+
cursor: pointer;
60+
}
61+
62+
.input-box input::-webkit-calendar-picker-indicator {
63+
top: 0;
64+
left: 0;
65+
right: 0;
66+
bottom: 0;
67+
width: auto;
68+
height: auto;
69+
position: absolute;
70+
background-position: calc(100% - 10px);
71+
background-size: 30px;
72+
cursor: pointer;
73+
}
74+
75+
#result {
76+
font-size: 1.25rem;
77+
}
78+
79+
#result span {
80+
color: #ffff76;
81+
}

0 commit comments

Comments
 (0)