Age calculator html code -Html code with java script for age calculator
Here is a code for age calculator online
<div style="text-align: center;">
<h2><br /></h2><h2><br /></h2><h2><br /></h2><h2>Age Calculator</h2>
<div>
<label>Date of Birth:</label>
<input id="dob" required="" type="date" />
</div>
<div>
<button onclick="calculateAge()">Calculate Age</button>
</div><div><div><br /></div><div><br /></div></div>
<div id="result" style="font-weight: bold; margin-top: 20px;"></div>
</div>
<script>
function calculateAge() {
var dob = new Date(document.getElementById('dob').value);
var today = new Date();
var ageInMilliseconds = today - dob;
var ageInSeconds = ageInMilliseconds / 1000;
var ageInMinutes = ageInSeconds / 60;
var ageInHours = ageInMinutes / 60;
var ageInDays = ageInHours / 24;
var ageInMonths = Math.floor(ageInDays / 30.44); // Average number of days in a month
var ageInYears = Math.floor(ageInMonths / 12);
var resultElement = document.getElementById('result');
resultElement.innerHTML = "Age: " +
ageInYears + " years, " +
ageInMonths % 12 + " months, and " +
Math.floor(ageInDays % 30.44) + " days.";
}
</script>
Comments
Post a Comment