Age Calculator


// Get the input values var birthDate = new Date(form.elements["birth-date"].value); var currentDate = new Date(form.elements["current-date"].value);

// Calculate the age in milliseconds var ageInMilliseconds = currentDate.getTime() - birthDate.getTime();

// Convert the age to years, months, and days var years = Math.floor(ageInMilliseconds / (1000 * 60 * 60 * 24 * 365.25)); var months = Math.floor((ageInMilliseconds % (1000 * 60 * 60 * 24 * 365.25)) / (1000 * 60 * 60 * 24 * 30.4)); var days = Math.floor(((ageInMilliseconds % (1000 * 60 * 60 * 24 * 365.25)) % (1000 * 60 * 60 * 24 * 30.4)) / (1000 * 60 * 60 * 24));

// Display the result document.getElementById("result").innerHTML = "Your age is: " + years + " years, " + months + " months, " + days + " days";

// Prevent the form from being submitted return false; }

Leave a Reply