Skip to content

Instantly share code, notes, and snippets.

@ajbm6
Created August 10, 2017 21:49
Show Gist options
  • Save ajbm6/bbc7ffe91d7fbb6dcc98217b779b016f to your computer and use it in GitHub Desktop.
Save ajbm6/bbc7ffe91d7fbb6dcc98217b779b016f to your computer and use it in GitHub Desktop.
Get age by date of birth (JavaScript)
/**
* 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;
};
// 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