JavaScript for calculating Age based on date
I am currently using JavaScript code to calculate the age based on the date of birth given by the user, it works fine but the display is not increasing correctly.
mycode:
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;
}
$(".date").on("change", function(){
var dob = $('.date').val();
var age = getAge(dob);
$('.age').val(age);
});
Here is the jsfiddle link Click here
If I give the date 21/03/1993 AM, getting the age is 20, but the correct age is 23.
source to share
Date order: mm / dd / yyyy or yyyy / mm / dd
So, change your code to:
var dob = new Date(dateString.substring(6,10),
dateString.substring(3,5),
dateString.substring(0,2)-1
);
Important suggestion:
In a javascript date object, the month starts from 0
, so your calculation is wrong.
Change code to:
if (monthNow >= monthDob)
var monthAge = monthNow - monthDob + 1; //<== add one here
else {
yearAge--;
var monthAge = 12 + monthNow -monthDob;
}
See Fiddle
source to share
Whenever I have anything with dates in Javascript, I tend to use Moment JS . This makes what you are trying to do much easier.
function age(birthDateString){
var now = moment();
var birthDate = moment('21/03/1993','DD/MM/YYYY');
var yearDiff = moment.duration(now - birthDateString).as('years');
return Math.floor(yearDiff);
}
Done - age('21/03/1993')
Returns22
source to share