Calculate age with years, months, and days using jquery

In this article we take birth date and using the “GetDate” function to find the years, months and days. So let’s have a look at a practical example of how to calculate age on client side using Javascript.

Date Format: MM/DD/YYYY

Write down below function in head tag.

Code:

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;
}

Example:

alert(getAge(’04/06/1988′));

 

Happy Coding 🙂

Advertisements

4 thoughts on “Calculate age with years, months, and days using jquery

  1. Hello a have a impprovements to your code and corrections:

    =================================

    function getAge(dateString) {
    var now = new Date();

    var today = new Date(now.getFullYear(),now.getMonth(),now.getDate());
    var dob = new Date(dateString.substring(6,10),dateString.substring(0,2)-1,dateString.substring(3,5));

    var yearNow = today.getFullYear();
    var monthNow = today.getMonth();
    var dateNow = today.getDate();

    var yearDob = dob.getFullYear();
    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–;//Decrementar un año
    var monthAge = 12 + monthNow – monthDob; //Diferencia de meses
    }

    if (dateNow >= dateDob)
    var dateAge = dateNow – dateDob;
    else {
    console.log(“monthNow: “+monthNow);
    // Calculate days in th motnh
    var daysInMonth = new Date(yearNow,monthNow+1,0).getDate();
    console.log(“Dias en mes: “+daysInMonth);

    monthAge–; //Decrementar un mes
    //var dateAge = 31 + dateNow – dateDob;
    var dateAge = daysInMonth + dateNow – dateDob;
    if (monthAge 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!”; //OK
    else if ( (age.years > 0) && (age.months == 0) && (age.days == 0) )
    ageString = age.years + yearString + ” old. Happy Birthday!!”; //OK
    else if ( (age.years == 0) && (age.months == 0) && (age.days == 0) )
    ageString = age.years + yearString + ” old. Has born now!!”; //OK
    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;
    }
    =========================================

  2. Another modification:

    Examples:
    getAge(‘2015-02-10′,’1980-09-18’)
    getAge(‘2015-02-10′,’1980-9-18’)
    getAge(‘2015-02-10′,’1980-01-18’)
    getAge(‘2015-02-10′,’1980-1-18’)

    where ‘2015-02-10’ is the now date and ‘1986-10-28’ is the birthday day of the person that we want to calculate the age.

    ‘2015-02-10’ can be retriben from a jQuery DatePicker or from PHP server via AJAX.

    ================================================ INI

    function getAge(now,dateString) {
    //var now = new Date(); //To test
    //var today = new Date(now.getFullYear(),now.getMonth(),now.getDate());

    var nowObj = now.split(‘-‘);
    var dateStringObj = dateString.split(‘-‘);
    for (var i=0; i<nowObj.length; i++) nowObj[i] = +nowObj[i];
    for (var i=0; i= monthDob)
    var monthAge = monthNow – monthDob;
    else {
    yearAge–;//Decrementar un año
    var monthAge = 12 + monthNow – monthDob; //Diferencia de meses
    }

    if (dateNow >= dateDob)
    var dateAge = dateNow – dateDob;
    else {
    // Calculate days in th month.
    // Using 0 in month we geth the las day of last month.
    var daysInMonth = new Date(yearNow,monthNow+1,0).getDate();
    monthAge–; //Decrementar un mes
    var dateAge = daysInMonth + dateNow – dateDob;
    if (monthAge 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!”; //OK
    else if ( (age.years > 0) && (age.months == 0) && (age.days == 0) )
    ageString = age.years + yearString + ” old. Happy Birthday!!”; //OK
    else if ( (age.years == 0) && (age.months == 0) && (age.days == 0) )
    ageString = age.years + yearString + ” old. Has born now!!”; //OK
    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;
    }

    ================================================== END

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s