Skip to content

Instantly share code, notes, and snippets.

@alfonsfoubert
Created October 4, 2013 16:33
Show Gist options
  • Save alfonsfoubert/6828789 to your computer and use it in GitHub Desktop.
Save alfonsfoubert/6828789 to your computer and use it in GitHub Desktop.
Javascript calculate age function
/**
* 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