Age Calculator
Age Calculator
Age Details:
Years: 0
Months: 0
Days: 0
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
background: linear-gradient(to bottom, #8e44ad, #3498db);
color: #fff;
}
.container {
max-width: 500px;
margin: 50px auto;
padding: 20px;
background: rgba(0, 0, 0, 0.6);
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
text-align: center;
}
h1 {
font-size: 2.5em;
margin-bottom: 20px;
}
.input-section {
margin-bottom: 20px;
}
label {
display: block;
font-size: 1.2em;
margin-bottom: 10px;
}
input[type="date"] {
padding: 10px;
width: 100%;
border: none;
border-radius: 5px;
margin-bottom: 10px;
}
button {
background: #27ae60;
color: #fff;
padding: 10px 15px;
border: none;
border-radius: 5px;
font-size: 1em;
cursor: pointer;
transition: 0.3s ease;
}
button:hover {
background: #2ecc71;
}
#result-section {
display: none;
background: rgba(255, 255, 255, 0.1);
padding: 20px;
border-radius: 5px;
margin-top: 20px;
}
#result-section p {
font-size: 1.2em;
margin: 10px 0;
}
#result-section span {
font-weight: bold;
color: #2ecc71;
}
@media (max-width: 768px) {
.container {
padding: 15px;
}
h1 {
font-size: 2em;
}
button {
width: 100%;
}
}
function calculateAge() {
const birthdateInput = document.getElementById('birthdate').value;
const resultSection = document.getElementById('result-section');
if (!birthdateInput) {
alert('Please select a valid birth date!');
return;
}
const birthdate = new Date(birthdateInput);
const today = new Date();
let years = today.getFullYear() - birthdate.getFullYear();
let months = today.getMonth() - birthdate.getMonth();
let days = today.getDate() - birthdate.getDate();
if (days < 0) {
months -= 1;
days += new Date(today.getFullYear(), today.getMonth(), 0).getDate();
}
if (months < 0) {
years -= 1;
months += 12;
}
// Display the result
document.getElementById('age-years').querySelector('span').textContent = years;
document.getElementById('age-months').querySelector('span').textContent = months;
document.getElementById('age-days').querySelector('span').textContent = days;
resultSection.style.display = 'block';
}