Skip to content

50Projects-HTML-CSS-JavaScript : Age calculator #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -210,6 +210,17 @@ In order to run this project you need:
</details>
</li>

<li>
<details>
<summary>Age Calculator</summary>
<p>Age Calculator is a user-friendly tool built using HTML, CSS, and JavaScript. The Age Calculator project is a beginner-friendly web development project designed to help users calculate their age effortlessly. Users can input their date of birth and instantly see their age in years.</p>
<ul>
<li><a href="https://tajulafreen.github.io/50Projects-HTML-CSS-JavaScript/Source-Code/AgeCalculator/">Live Demo</a></li>
<li><a href="https://github.com/tajulafreen/50Projects-HTML-CSS-JavaScript/tree/main/Source-Code/AgeCalculator">Source</a></li>
</ul>
</details>
</li>

</ol>

<p align="right">(<a href="#readme-top">back to top</a>)</p>
23 changes: 23 additions & 0 deletions Source-Code/AgeCalculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!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="center">
<div class="container">
<p id="currDate"></p>
<p>Enter the DOB in format: (MM/DD/YYYY)</p>
<input type="text" placeholder="Enter your D.O.B" id="DOB" />
<button id="CalcAge">Calculate Age</button>
</div>
<div id="displayAge">
<p id="age"></p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
26 changes: 26 additions & 0 deletions Source-Code/AgeCalculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
document.addEventListener('DOMContentLoaded', () => {
const currDate = document.getElementById('currDate');
const dateOfBirth = document.querySelector('#DOB');
const calcAgeButton = document.getElementById('CalcAge');
const displayAge = document.getElementById('displayAge');
const ageText = document.getElementById('age');
const today = new Date();

currDate.innerText = `Today's Date is: ${today.toLocaleDateString('en-US')}`;

calcAgeButton.addEventListener('click', () => {
const birthDate = new Date(dateOfBirth.value);
let age = today.getFullYear() - birthDate.getFullYear();
const monthDifference = today.getMonth() - birthDate.getMonth();

if (
monthDifference < 0
|| (monthDifference === 0 && today.getDate() < birthDate.getDate())
) {
age -= 1;
}

displayAge.style.visibility = 'visible';
ageText.innerText = `You are ${age} years old.`;
});
});
82 changes: 82 additions & 0 deletions Source-Code/AgeCalculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: cursive;
}

.center {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.container {
display: flex;
width: 600px;

/* margin:auto;
margin-top:10%;
margin-bottom:10%; */
align-items: center;
justify-content: center;
flex-direction: column;
background-color: rgb(169, 216, 80);
box-shadow: 8px 8px black;
color: white;
padding: 5% 0%;
}

#currDate {
font-size: 30px;
margin: 20px;
font-weight: bold;
}

input {
font-size: 20px;
padding: 15px;
margin: 20px;
text-align: center;
border-radius: 20px;
border: 1px solid yellow;
cursor: pointer;
}

button {
font-size: 20px;
padding: 10px 20px;
border-radius: 10px;
border: none;
background-color: yellow;
color: black;
margin: 20px;
text-transform: uppercase;
font-weight: bold;
cursor: pointer;
}

button:hover {
background-color: white;
color: rgb(169, 216, 80);
}

#displayAge {
display: flex;
align-items: center;
justify-content: center;
width: 620px;
height: 490px;
background-color: rgb(91, 228, 141);
border-radius: 30px;
position: absolute;
visibility: hidden;
}

#age {
color: white;
font-size: 50px;
margin: 20px;
font-weight: bold;
}