body {
font-family: ‘Arial’, sans-serif;
background-color: #f4f4f4;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
#age-calculator {
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
overflow: hidden;
width: 300px;
margin: auto;
}
#input-container {
padding: 20px;
text-align: center;
}
label {
font-size: 16px;
display: block;
margin-bottom: 5px;
}
input {
width: 100%;
padding: 8px;
margin-bottom: 15px;
}
button {
border: none;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #4caf50;
color: #fff;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
#result {
padding: 20px;
text-align: center;
font-size: 18px;
font-weight: bold;
}
function calculateAge() {
const birthdate = document.getElementById(‘birthdate’).value;
if (!birthdate) {
alert(‘Please enter your birthdate.’);
return;
}
const birthDateObj = new Date(birthdate);
const currentDate = new Date();
const ageInMilliseconds = currentDate – birthDateObj;
const ageInYears = Math.floor(ageInMilliseconds / (365.25 * 24 * 60 * 60 * 1000));
document.getElementById(‘result’).innerText = `Your age is ${ageInYears} years.`;
}