Javascript Date() to calculate age work by the day,months,years
function getAge(dateString) {
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate());
var yearNow = now.getYear();
var monthNow = now.getMonth();
var dateNow = now.getDate();
var dob = new Date(dateString.substring(6,10),
dateString.substring(0,2)-1,
dateString.substring(3,5)
);
var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate();
var age = {};
var ageString = "";
var yearString = "";
var monthString = "";
var dayString = "";
yearAge = yearNow - yearDob;
if (monthNow >= monthDob)
var monthAge = monthNow - monthDob;
else {
yearAge--;
var monthAge = 12 + monthNow -monthDob;
}
if (dateNow >= dateDob)
var dateAge = dateNow - dateDob;
else {
monthAge--;
var dateAge = 31 + dateNow - dateDob;
if (monthAge < 0) {
monthAge = 11;
yearAge--;
}
}
age = {
years: yearAge,
months: monthAge,
days: dateAge
};
if ( age.years > 1 ) yearString = " years";
else yearString = " year";
if ( age.months> 1 ) monthString = " months";
else monthString = " month";
if ( age.days > 1 ) dayString = " days";
else dayString = " day";
if ( (age.years > 0) && (age.months > 0) && (age.days > 0) )
ageString = age.years + yearString + ", " + age.months + monthString + ", and " + age.days + dayString + " old.";
else if ( (age.years == 0) && (age.months == 0) && (age.days > 0) )
ageString = "Only " + age.days + dayString + " old!";
else if ( (age.years > 0) && (age.months == 0) && (age.days == 0) )
ageString = age.years + yearString + " old. Happy Birthday!!";
else if ( (age.years > 0) && (age.months > 0) && (age.days == 0) )
ageString = age.years + yearString + " and " + age.months + monthString + " old.";
else if ( (age.years == 0) && (age.months > 0) && (age.days > 0) )
ageString = age.months + monthString + " and " + age.days + dayString + " old.";
else if ( (age.years > 0) && (age.months == 0) && (age.days > 0) )
ageString = age.years + yearString + " and " + age.days + dayString + " old.";
else if ( (age.years == 0) && (age.months > 0) && (age.days == 0) )
ageString = age.months + monthString + " old.";
else ageString = "Oops! Could not calculate age!";
return ageString;
}
alert(getAge('09/09/1989'));
DEMO HERE
Calculates age in terms of years, months and days. Enter the dates in any valid date string format such as '1952/09/28', 'Sep 29, 1952', '09/28/1952' etc.
Takes 2 arguments - date of birth and the date on which to calculate age. You can leave the second argument out for today's date. Returns an object with years, months and days properties of age.
Uses the solar year value of 365.2425 days in a year.
@param birthDate Date of birth. @param ageAtDate The date on which to calculate the age. None for today's date. @returns {{years: number, months: number, days: number}}
function getAge(birthDate, ageAtDate) {
var daysInMonth = 30.436875; // Days in a month on average.
var dob = new Date(birthDate);
var aad;
if (!ageAtDate) aad = new Date();
else aad = new Date(ageAtDate);
var yearAad = aad.getFullYear();
var yearDob = dob.getFullYear();
var years = yearAad - yearDob; // Get age in years.
dob.setFullYear(yearAad); // Set birthday for this year.
var aadMillis = aad.getTime();
var dobMillis = dob.getTime();
if (aadMillis < dobMillis) {
--years;
dob.setFullYear(yearAad - 1); // Set to previous year's birthday
dobMillis = dob.getTime();
}
var days = (aadMillis - dobMillis) / 86400000;
var monthsDec = days / daysInMonth; // Months with remainder.
var months = Math.floor(monthsDec); // Remove fraction from month.
days = Math.floor(daysInMonth * (monthsDec - months));
return {years: years, months: months, days: days};
}