Age Calculator
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Poppins', sans-serif;
background: linear-gradient(to right, #ff7e5f, #feb47b);
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
color: #fff;
}
.container {
background-color: rgba(0, 0, 0, 0.6);
padding: 30px;
border-radius: 10px;
text-align: center;
max-width: 400px;
width: 100%;
}
h1 {
font-size: 2.5rem;
margin-bottom: 20px;
}
label {
font-size: 1.1rem;
margin-bottom: 10px;
display: block;
}
input[type="date"] {
padding: 10px;
font-size: 1rem;
border: 1px solid #ddd;
border-radius: 5px;
width: 100%;
margin-bottom: 20px;
}
button {
background-color: #ff5f6d;
color: white;
font-size: 1rem;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #feb47b;
}
.result {
margin-top: 20px;
font-size: 1.5rem;
font-weight: 600;
}
document.getElementById('ageForm').addEventListener('submit', function(event) {
event.preventDefault();
let birthDate = document.getElementById('birthdate').value;
if (!birthDate) {
alert('Please enter your birth date');
return;
}
let birthDateObj = new Date(birthDate);
let today = new Date();
let age = today.getFullYear() - birthDateObj.getFullYear();
let monthDifference = today.getMonth() - birthDateObj.getMonth();
if (monthDifference < 0 || (monthDifference === 0 && today.getDate() < birthDateObj.getDate())) {
age--;
}
let ageResult = document.getElementById('ageResult');
ageResult.innerHTML = `You are ${age} years old.`;
});