Created
October 4, 2013 16:33
-
-
Save alfonsfoubert/6828789 to your computer and use it in GitHub Desktop.
Javascript calculate age function
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
/** | |
* Tells you the age from a string date | |
* | |
* @param dateString Date in string format ex: "1981/3/27" | |
* | |
* @return integer The calculated age | |
**/ | |
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