Skip to content

Commit 23e7a46

Browse files
authored
Merge pull request #91 from Gaurav0203Shetty/age-calculator
Age calculator added
2 parents c760fb4 + 63f98cf commit 23e7a46

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

Age-calculator/.DS_Store

6 KB
Binary file not shown.

Age-calculator/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Age calculator website-
2+
3+
This is a simple Age calculator website created with the help of HTML, CSS and JS.\
4+
It not only gives us the year but also the number of days and months!\
5+
6+
Feel free to improve this code if interested!}

Age-calculator/age_cal.css

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: 'Poppins', sans-serif;
5+
box-sizing: border-box;
6+
}
7+
8+
.container{
9+
width: 100%;
10+
min-height: 100vh;
11+
background: linear-gradient(135deg, rgb(213, 3, 3), yellow);
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: 60px;
25+
}
26+
27+
.calculator h1 span{
28+
color: rgb(55, 244, 3);
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: rgb(55, 244, 3);
53+
border: 0;
54+
outline: 0;
55+
padding: 15px 30px;
56+
border-radius: 5px;
57+
font-weight: 500;
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: 20px;
77+
}
78+
79+
#result span{
80+
color: rgb(55, 244, 3);
81+
}

Age-calculator/age_cal.html

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

Age-calculator/age_cal.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
let userInput = document.getElementById("date")
2+
userInput.max = new Date().toISOString().split("T")[0];
3+
let result = document.getElementById("result");
4+
function calculateAge(){
5+
let birthDate = new Date(userInput.value)
6+
let d1 = birthDate.getDate();
7+
let m1 = birthDate.getMonth() + 1;
8+
let y1 = birthDate.getFullYear();
9+
let today = new Date()
10+
let d2 = today.getDate();
11+
let m2 = today.getMonth() + 1;
12+
let y2 = today.getFullYear();
13+
let d3, m3, y3;
14+
y3 = y2 - y1;
15+
if(m2 > m1)
16+
{
17+
m3 = m2 - m1;
18+
}
19+
else{
20+
y3--;
21+
m3 = 12 + m2 - m1;
22+
}
23+
if(d2 >= d1){
24+
d3 = d2 - d1;
25+
}
26+
else{
27+
m3--;
28+
d3 = getDaysInMonth(y1 , m1) + d2 - d1;
29+
}
30+
if(m3<0){
31+
m3 = 11;
32+
y3--;
33+
}
34+
result.innerHTML = `You are <span>${y3}</span> years, <span>${m3}</span> months and <span>${d3}</span> days old`;
35+
}
36+
37+
function getDaysInMonth(year,month){
38+
return new Date(year, month, 0).getDate();
39+
}

0 commit comments

Comments
 (0)