Wednesday, April 30, 2014

JQUERY: CalculationAge only in years

function CalculationAge() {
        if ($("#DOB").val() != "") {
            var today = new Date(), // today date object
      birthday_val = $("#DOB").val().split('/'), // input value
      birthday = new Date(birthday_val[2], birthday_val[0] - 1, birthday_val[1]), // birthday date object
      todayYear = today.getFullYear(), // today year
      todayMonth = today.getMonth(), // today month
      todayDay = today.getDate(), // today day of month
      birthdayYear = birthday.getFullYear(), // birthday year
      birthdayMonth = birthday.getMonth(), // birthday month
      birthdayDay = birthday.getDate(), // birthday day of month
            // calculate age
      age = (todayMonth == birthdayMonth && todayDay > birthdayDay) ? todayYear - birthdayYear : (todayMonth > birthdayMonth) ? todayYear - birthdayYear : todayYear - birthdayYear - 1;

            $("#Age").val(age);
        }
        else
            $("#Age").val('0');
    }

No comments:

Post a Comment