Created
June 13, 2016 14:57
-
-
Save MichaelPolla/d60d1b30aa4f2a908c47cb36221df625 to your computer and use it in GitHub Desktop.
[JS] Calculate age (years) from a date
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
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