Age Calculator
Flp Duniya –
Flp Duniya –
// 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 = `
${ageYears} Years, ${ageMonths} Months, and ${ageDays} Days
Total Months: ${(ageYears*12)+ageMonths}
Total Days: ${Math.floor((today - birthdate)/(1000*60*60*24))}
`; }