Calculate years from date using javascript

Below is the javascript function which can be used to calculate the number of years from a specific date, Age from Date of birth,...

function calculateage() {

var fromdate= document.myform.fromdate;

         if (fromdate.value != '') {

            now = new Date()

            fromdate = fromdate.value.split('-');

            if (fromdate.length === 3) {

                born = new Date(dob[2], dob[1] * 1 - 1, dob[0]);

                age = Math.floor((now.getTime() - born.getTime()) / (365.25 * 24 * 60 * 60 * 1000));

                if(isNaN(age))

                {

                   document.getElementById('lblAge').innerHTML='';

                   alert('Input date is incorrect!');

                }

                else

                {

               document.myform.age.value=age;

                }

            }

        }

    }

No comments:

Post a Comment