Javascript function to calculate age from date of birth entered in a text box

Solution

function calculateAge() 
{ 
var dob = $('#DateOfBirth').val();
 dob = new Date(dob);
 var today = new Date();
 var age = Math.floor((today - dob) / (365.25 * 24 * 60 * 60 * 1000));
 $('#Age').val(age); 
}

Explanation

dob will get the date of birth from DateOfBirth text box. It will convert it into date so to calculate further. today will store the current date. The result of math.floor function will be stored in age variable and the value of age will be displayed in the Age field.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started