Features of This Age Calculator

author
1 minute, 2 seconds Read

Age Calculator


// Check valid date if(isNaN(birthdate.getTime())) { alert("Please select a valid date!"); return; }

// Prevent future dates if(birthdate > today) { alert("Birthdate cannot be in future!"); return; }

// Calculate age let ageYears = today.getFullYear() - birthdate.getFullYear(); let ageMonths = today.getMonth() - birthdate.getMonth(); let ageDays = today.getDate() - birthdate.getDate();

// Adjust for month/day differences if (ageDays < 0) { ageMonths--; const tempDate = new Date(today.getFullYear(), today.getMonth(), 0); ageDays += tempDate.getDate(); } if (ageMonths < 0) { ageYears--; ageMonths += 12; } // Display result const resultDiv = document.getElementById('result'); resultDiv.style.display = 'block'; resultDiv.innerHTML = `

Your Age is:

${ageYears} Years, ${ageMonths} Months, and ${ageDays} Days

Total Months: ${(ageYears*12)+ageMonths}

Total Days: ${Math.floor((today - birthdate)/(1000*60*60*24))}

`; }

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *