function test(val) {
year = parseInt(val.slice(0,2)); // get year
month = parseInt(val.slice(2,4)); // get month
date = val.slice(4,6); // get date
if (month > 40) { // For people born after 2000, 40 is added to the month.
year += 2000;
month -= 40;
} else {
year += 1900;
}
date = new Date(year, month-1, date, 0, 0);
date_now = new Date();
var diff =(date_now.getTime() - date.getTime()) / 1000;
diff /= (60 * 60 * 24);
diff = Math.abs(Math.floor(diff/365.25));
console.log(diff);
}
test("940911") // Should return 25
test("940910") // Should return 26
test("940909") // Should return 26
So If I'm born 1994.09.11 function should returns 25, Because 09.11 is tommorrow, but if I'm born at 1994.09.09, It should returns 26, because 09.09 was yesterday. I do not want to use libreries like moment.js etc.