Everything naveen wrote in this post makes sense: Calculate age given the birth date in the format YYYYMMDD
...besides the last bit with the if (m < 0)-part. Could anyone explain it to me? So far I get that: if month is less than 0, then why take the age and reduce by 1?
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;
}