Created
February 23, 2015 13:25
-
-
Save andreruffert/c5ef9452ea4aa2e5f3f4 to your computer and use it in GitHub Desktop.
Get age from a dateString
This file contains hidden or 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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment