Skip to content

Commit

Permalink
Merge pull request #91 from Gaurav0203Shetty/age-calculator
Browse files Browse the repository at this point in the history
Age calculator added
  • Loading branch information
shrey141102 authored Dec 5, 2023
2 parents c760fb4 + 63f98cf commit 23e7a46
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
Binary file added Age-calculator/.DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions Age-calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Age calculator website-

This is a simple Age calculator website created with the help of HTML, CSS and JS.\
It not only gives us the year but also the number of days and months!\

Feel free to improve this code if interested!}
81 changes: 81 additions & 0 deletions Age-calculator/age_cal.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
*{
margin: 0;
padding: 0;
font-family: 'Poppins', sans-serif;
box-sizing: border-box;
}

.container{
width: 100%;
min-height: 100vh;
background: linear-gradient(135deg, rgb(213, 3, 3), yellow);
color: #fff;
padding: 10px;
}

.calculator{
width: 100%;
max-width: 600px;
margin-left: 10%;
margin-top: 10%;
}

.calculator h1{
font-size: 60px;
}

.calculator h1 span{
color: rgb(55, 244, 3);
}

.input-box{
margin: 40px 0;
padding: 35px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.3);
display: flex;
align-items: center;
}

.input-box input{
flex: 1;
margin-right: 20px;
padding: 14px 20px;
border: 0;
outline: 0;
font-size: 18px;
border-radius: 5px;
position: relative;
}

.input-box button{
background: rgb(55, 244, 3);
border: 0;
outline: 0;
padding: 15px 30px;
border-radius: 5px;
font-weight: 500;
color: #333;
cursor: pointer;
}

.input-box input::-webkit-calendar-picker-indicator{
top: 0;
left: 0;
right: 0;
bottom: 0;
width: auto;
height: auto;
position: absolute;
background-position: calc(100% - 10px);
background-size: 30px;
cursor: pointer;
}

#result{
font-size: 20px;
}

#result span{
color: rgb(55, 244, 3);
}
22 changes: 22 additions & 0 deletions Age-calculator/age_cal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!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="style.css">
</head>
<body>
<div class="container">
<div class="calculator">
<h1>Simple Javascript <br><span> age calculator</span></h1>
<div class="input-box">
<input type="date" id="date">
<button onclick="calculateAge()">Calculate</button>
</div>
<p id="result"></p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions Age-calculator/age_cal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
let userInput = document.getElementById("date")
userInput.max = new Date().toISOString().split("T")[0];
let result = document.getElementById("result");
function calculateAge(){
let birthDate = new Date(userInput.value)
let d1 = birthDate.getDate();
let m1 = birthDate.getMonth() + 1;
let y1 = birthDate.getFullYear();
let today = new Date()
let d2 = today.getDate();
let m2 = today.getMonth() + 1;
let y2 = today.getFullYear();
let d3, m3, y3;
y3 = y2 - y1;
if(m2 > m1)
{
m3 = m2 - m1;
}
else{
y3--;
m3 = 12 + m2 - m1;
}
if(d2 >= d1){
d3 = d2 - d1;
}
else{
m3--;
d3 = getDaysInMonth(y1 , m1) + d2 - d1;
}
if(m3<0){
m3 = 11;
y3--;
}
result.innerHTML = `You are <span>${y3}</span> years, <span>${m3}</span> months and <span>${d3}</span> days old`;
}

function getDaysInMonth(year,month){
return new Date(year, month, 0).getDate();
}

0 comments on commit 23e7a46

Please sign in to comment.