Here is my code, as taken from here: Calculate age in JavaScript
var today = new Date();
birthday_val = $(".datepicker").val().split('/'); // input value
birthday = new Date(birthday_val[2],birthday_val[1],birthday_val[0]); // birthday date object
var age = today.getFullYear() - birthday.getFullYear();
var m = today.getMonth() - birthday.getMonth();
var d = today.getDay() - birthday.getDay();
if (m < 0 || (m === 0 && today.getDate() < birthday.getDate()) ) {
age--;
}
It calculates it right except for the day – doesnt take this into account and I cant figure out why. Any takers?
Edit: This isn’t the correct answer, despite being marked so by the OP.
You’re using the incorrect method to retrieve the day of the month.
Date.getDayreturns the day of the weekDate.getDatereturns the day of the month