age creator
function calculateAge(event) {
event.preventDefault();
const dob = new Date(document.getElementById('dob').value);
const today = new Date();
// Calculate age components
let ageInYears = today.getFullYear() - dob.getFullYear();
let ageInMonths = today.getMonth() - dob.getMonth();
let ageInDays = today.getDate() - dob.getDate();
// Adjust for negative months/days
if (ageInDays < 0) {
ageInDays += new Date(today.getFullYear(), today.getMonth(), 0).getDate();
ageInMonths--;
}
if (ageInMonths < 0) {
ageInMonths += 12;
ageInYears--;
}
// Calculate total values
const totalDays = Math.floor((today - dob) / (1000 * 60 * 60 * 24));
const totalWeeks = Math.floor(totalDays / 7);
const totalMonths = (ageInYears * 12) + ageInMonths;
const totalHours = totalDays * 24;
const totalMinutes = totalHours * 60;
const totalSeconds = totalMinutes * 60;
// Display results
document.getElementById('age-results').innerHTML = `
Age:
${ageInYears} years ${ageInMonths} months ${ageInDays} days
or ${totalMonths} months ${ageInDays} days
or ${totalWeeks} weeks ${ageInDays} days
or ${totalDays} days
or ${totalHours} hours
or ${totalMinutes} minutes
or ${totalSeconds} seconds
`; }
Comments
Post a Comment