Skip to content
Open
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
100 changes: 100 additions & 0 deletions new-age-calculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!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>
<style>
body {
font-family: "Poppins", sans-serif;
background: linear-gradient(135deg, #00c6ff, #0072ff);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
margin: 0;
}

.container {
background: #fff;
padding: 30px;
border-radius: 15px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
text-align: center;
width: 320px;
}

h1 {
margin-bottom: 20px;
color: #0072ff;
font-size: 24px;
}

input[type="date"] {
padding: 10px;
width: 100%;
border: 2px solid #0072ff;
border-radius: 8px;
font-size: 16px;
outline: none;
transition: 0.3s;
}

input[type="date"]:focus {
border-color: #00c6ff;
}

button {
margin-top: 15px;
padding: 10px 20px;
background: #0072ff;
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
}

button:hover {
background: #0059d6;
}

#result {
margin-top: 20px;
font-size: 18px;
font-weight: 600;
color: #333;
}
</style>
</head>
<body>
<div class="container">
<h1>Age Calculator</h1>
<input type="date" id="dob" />
<button onclick="calculateAge()">Calculate Age</button>
<div id="result"></div>
</div>

<script>
function calculateAge() {
const dob = document.getElementById("dob").value;
if (!dob) {
document.getElementById("result").innerText = "Please select your date of birth!";
return;
}

const birthDate = new Date(dob);
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();

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

document.getElementById("result").innerText = `Your age is ${age} years old 🎉`;
}
</script>
</body>
</html>