Created
August 10, 2017 21:49
-
-
Save ajbm6/bbc7ffe91d7fbb6dcc98217b779b016f to your computer and use it in GitHub Desktop.
Get age by date of birth (JavaScript)
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
/** | |
* Returns age from date of birth | |
* | |
* @param {Date} date | |
* @returns {Number} | |
*/ | |
function getAge(date) { | |
var now = new Date(); | |
var age = now.getFullYear() - date.getFullYear(); | |
return age; | |
}; |
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
// iso date time | |
var dateOfBirth = new Date('1982-03-28 00:00:00'); | |
// calculate age | |
var age = getAge(dateOfBirth); | |
// output | |
console.log(age); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment