Skip to content

Instantly share code, notes, and snippets.

@bmodena
Created December 8, 2020 19:15
Show Gist options
  • Save bmodena/559107976f2a7b394cf3c06be3027fd5 to your computer and use it in GitHub Desktop.
Save bmodena/559107976f2a7b394cf3c06be3027fd5 to your computer and use it in GitHub Desktop.
Javascript Get Age for over 18 or over 21 DOB validation
/*
* Pass a date string into the function and return the age
* dateString = MM/DD/YYYY
---------------------
*/
function getAge(dateString) {
var today = new Date(),
birthDate = new Date(dateString),
age = today.getFullYear() - birthDate.getFullYear(),
m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment