Last Updated: February 25, 2016
·
441
· andrelug

Get age from birth date snippet

I came across this snippet and I found it very useful! You pass the birth date and it returns the age.

function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}