Created
December 8, 2020 19:15
-
-
Save bmodena/559107976f2a7b394cf3c06be3027fd5 to your computer and use it in GitHub Desktop.
Javascript Get Age for over 18 or over 21 DOB validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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