Skip to content

Instantly share code, notes, and snippets.

@MichaelPolla
Created June 13, 2016 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelPolla/d60d1b30aa4f2a908c47cb36221df625 to your computer and use it in GitHub Desktop.
Save MichaelPolla/d60d1b30aa4f2a908c47cb36221df625 to your computer and use it in GitHub Desktop.
[JS] Calculate age (years) from a date
function getAge(dateString) {
var birthDate = new Date(dateString);
var today = new Date();
var diff = today-birthDate; // Difference in milliseconds
var age = Math.floor(diff/31557600000); // Divide by 1000*60*60*24*365.25 <- length of a year (365 days and 6 hours) in milliseconds
return age;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment