The calculateAge
function performs the following tasks:
- Get User Input:
- Retrieves the user's birthdate from an HTML input element with the id 'birthdate'.
let userDate = document.getElementById('birthdate').value;
- Check Validity of User Input:
- Checks if the entered value is not a number (NaN) or an empty string. If invalid, it displays an alert and stops further execution.
if (userDate == NaN || userDate == '') {
return alert('Enter a valid date of birth. You may have not entered a complete date or entered a non-existent date information.');
}
- Create Date Objects:
- Converts the user's birthdate and the current date into
Date
objects.
- Converts the user's birthdate and the current date into
const birthdate = new Date(userDate);
const currentDate = new Date();
- Reset Result and Error Display:
- Hides the result and error elements on the HTML page.
document.getElementById('result').style.display = 'none';
document.getElementById('error').style.display = 'none';
- Check Birthdate Validity:
- If the birthdate is greater than the current date, it displays an error message.
if (birthdate > currentDate) {
document.getElementById('error').style.display = 'block';
document.getElementById('error').innerHTML = 'Please enter a valid birthdate.';
return;
}
- Calculate Age Difference:
- Calculates the age difference between the birthdate and the current date in various units (milliseconds, years, months, weeks, days, hours, minutes, seconds).
const ageInMilliseconds = currentDate - birthdate;
const millisecondsInYear = 1000 * 60 * 60 * 24 * 365.25;
const millisecondsInMonth = millisecondsInYear / 12;
const millisecondsInWeek = 7 * 24 * 60 * 60 * 1000;
// ... (similar calculations for other time units)
- Display Age Information:
- Updates the HTML elements with the calculated age values.
document.getElementById('years').textContent = years;
document.getElementById('months').textContent = months;
// ... (similar updates for other time units)
- Calculate and Display Life Milestones:
- Calculates and displays life milestones like retirement age, half-life, and specific ages.
const retirementAge = 65;
const halfLife = years / 2;
const age18 = 18;
const age21 = 21;
const age30 = 30;
document.getElementById('retirementAge').textContent = retirementAge;
document.getElementById('halfLife').textContent = halfLife.toFixed(2); // Show with two decimal places
// ... (similar updates for other milestones)
- Additional Notes:
- The code provides flexibility to add more milestones as needed.