Skip to content

Instantly share code, notes, and snippets.

@sandbochs
Created September 16, 2013 06:27
Show Gist options
  • Save sandbochs/6577214 to your computer and use it in GitHub Desktop.
Save sandbochs/6577214 to your computer and use it in GitHub Desktop.
getAge = function (birthDate, nowDate) {
var today, age, mDiff, dDiff;
today = nowDate || new Date();
age = nowDate.getFullYear() - birthDate.getFullYear();
mDiff = nowDate.getMonth() - birthDate.getMonth();
dDiff = nowDate.getDate() - birthDate.getDate();
if (mDiff < 0 || (mDiff === 0 && dDiff < 0)) {
age -= 1;
}
return age
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment