Skip to content

Instantly share code, notes, and snippets.

@gsingharoy
Created November 10, 2014 09:47
Show Gist options
  • Save gsingharoy/296fb549d33ecb05a697 to your computer and use it in GitHub Desktop.
Save gsingharoy/296fb549d33ecb05a697 to your computer and use it in GitHub Desktop.
Returns the integer current age from the D.O.B. string which is in format 'YYYY-MM-DD'
//dob is of format 'YYYY<separator>MM<separator>DD'
//eg, to find the age with DOB string 1969-08-02 we need to call the method getAge('1969-08-02','-')
function getAge(dob,separator){
var arr_d = dob.split(separator);
var date_d = new Date(parseInt(arr_d[0]),parseInt(arr_d[1])-1,parseInt(arr_d[2]));//In javaScript month values are from 0-11. So January is 0.
var date_now = Date.now();
var one_year=1000*60*60*24*365; //getting the millisecond in one year
var str_age = ((date_now-date_d)/one_year).toString();
return parseInt(str_age.split(".")[0]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment