Kitek's Page Blog programisty / webdeveloper'a

Obliczanie wieku w JavaScript

Prosta funkcja przetwarzająca datę urodzin na wiek np: 1985-04-11 –> 26 lat

var getAgeStr = function(dateString) {
    var myDate = new Date(dateString), 
        now = new Date(),
        month_of_birth=myDate.getMonth(),
        day_of_birht=myDate.getDay(),
        year_of_birth=myDate.getYear(),
        now_month = now.getMonth(),
        now_day = now.getDay(),
        now_year = now.getYear(),
        age = now_year - year_of_birth;
    if (now_month < month_of_birth) {
        age--;
    } else if ((now_month == month_of_birth) && (now_day < day_of_birht)) {
        age--;
        if (age < 0) {
            age = 0;
        }
    }
    return age;
}