Calculate age from date using javascript

Below is the javascript function which can be used to calculate age from date

//Calculate age from DOB (dd/mm/yyyy)
//This function can be customized to any other date formats
function calculate_age(yearinput){
      
        var year=yearinput;
        var str=year.split('/');

        var date=parseInt(str[0]);
        var month=parseInt(str[1]);
        var year=parseInt(str[2]);

        var myDate=new Date();
        myDate.setFullYear(year, month, date);
      
        var today = new Date();
                  
            var bday=date;
            var bmo=(month-1);
            var byr=year;
            var byr;
            var age;
            var now = new Date();
            tday=now.getDate();
            tmo=(now.getMonth());
            tyr=(now.getFullYear());
          
            if((tmo > bmo)||(tmo==bmo & tday>=bday))
            {    age=byr    }
            else
            {    age=byr+1}
          
            return (tyr-age);
}

No comments:

Post a Comment