Having 2 javascript Dates,
first is birthdate and second is a date to calculate age from that date.
What should be the best way to do this.
function calculateAge (birthDate, otherDate) { birthDate = new Date(birthDate); otherDate = new Date(otherDate); var years = (otherDate.getFullYear() - birthDate.getFullYear()); if (otherDate.getMonth() < birthDate.getMonth() || otherDate.getMonth() == birthDate.getMonth() && otherDate.getDate() < birthDate.getDate()) { years--; } return years; }
Example:
var age = calculateAge("02/24/1991", "02/24/2010"); // Format: MM/DD/YYYY
2.1m questions
2.1m answers
60 comments
57.0k users