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.
See Question&Answers more detail:osHaving 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.
See Question&Answers more detail:osfunction 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