3

I have a textfield where the user enters the date of birth in mm/dd/yyyy format. I want to autofill the age field for the user based on their entered dob.

the age textfield should be populated when user tabs out of the dob field.

what is the jQuery way of doing this?

Omnipresent
  • 29,434
  • 47
  • 142
  • 186

6 Answers6

3

See Calculate age in JavaScript for a function to calculate the age that handles leap years appropriately.

You can then use that function to calculate the user's age, like so:

var age = getAge(new Date($("#date").value()));

.. and then set the age field to that number:

$("#date").bind("blur", function() { $("#age").value(age) });
Community
  • 1
  • 1
0

I came up with a simple solution for this. It's an jQuery change function that sends the value to an input age field.

$(document).ready(function() {

    $('#dob input').change(function() {

        var today = new Date();
        var dd = Number(today.getDate());
        var mm = Number(today.getMonth() + 1);

        var yyyy = Number(today.getFullYear());

        var myBD = $('#dob input').val();
        var myBDM = Number(myBD.split("/")[0])
        var myBDD = Number(myBD.split("/")[1])
        var myBDY = Number(myBD.split("/")[2])
        var age = yyyy - myBDY;

        if (mm < myBDM) {
            age = age - 1;
        } else if (mm == myBDM && dd < myBDD) {
            age = age - 1;
        }

        $('#age input').val(age);
    });

});
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
0

Here we test it by taking start and end date. You can also use it to calculate age in months and days.

var end = new Date(2015, 09, 02, 00, 0, 0, 0),
    begin = new Date(2015, 09, 1, 00, 0, 0, 0),
    e = new Date(end),
    b = new Date(begin),
    bMonth = b.getMonth(),
    bYear = b.getFullYear(),
    eYear = e.getFullYear(),
    eMonth = e.getMonth(),
    bDay = b.getDate(),
    eDay = e.getDate() + 1;

if ((eMonth == 0) || (eMonth == 2) || (eMonth == 4) || (eMonth == 6) ||
    (eMonth == 7) || (eMonth == 9) || (eMonth == 11)) {
    var eDays = 31;
}

if ((eMonth == 3) || (eMonth == 5) || (eMonth == 8) || (eMonth == 10)) {
    var eDays = 30;
}

if (eMonth == 1 && ((eYear % 4 == 0) && (eYear % 100 != 0)) ||
    (eYear % 400 == 0)) {
    var eDays = 29;
}

if (eMonth == 1 && ((eYear % 4 != 0) || (eYear % 100 == 0))) {
    var eDays = 28;
}

if ((bMonth == 0) || (bMonth == 2) || (bMonth == 4) || (bMonth == 6) ||
    (bMonth == 7) || (bMonth == 9) || (bMonth == 11)) {
    var bDays = 31;
}

if ((bMonth == 3) || (bMonth == 5) || (bMonth == 8) || (bMonth == 10)) {
    var bDays = 30;
}

if (bMonth == 1 && ((bYear % 4 == 0) &&
        (bYear % 100 != 0)) || (bYear % 400 == 0)) {
    var bDays = 29;
}

if (bMonth == 1 && ((bYear % 4 != 0) || (bYear % 100 == 0))) {
    var bDays = 28;
}

var FirstMonthDiff = bDays - bDay + 1;

if (eDay - bDay < 0) {
    eMonth = eMonth - 1;
    eDay = eDay + eDays;
}

var daysDiff = eDay - bDay;

if (eMonth - bMonth < 0) {
    eYear = eYear - 1;
    eMonth = eMonth + 12;
}

var monthDiff = eMonth - bMonth,
    yearDiff = eYear - bYear;

if (daysDiff == eDays) {
    daysDiff = 0;
    monthDiff = monthDiff + 1;

    if (monthDiff == 12) {
        monthDiff = 0;
        yearDiff = yearDiff + 1;
    }
}

if ((FirstMonthDiff != bDays) && (eDay - 1 == eDays)) {
    daysDiff = FirstMonthDiff;
}
console.log(yearDiff + " Year(s)" + " " +
    monthDiff + " month(s) " + daysDiff + " days(s)");    
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
0
function (row) 
{
    var age = 0;
    var birthday = row["FechaNacimiento"];

    age = new Date().getFullYear() - birthday.getFullYear();

    if (new Date() < birthday.setFullYear(birthday.getFullYear() + age)) 
    {
        age--;
    }

    return age;
}

late but this works...

Daniel Corzo
  • 1,055
  • 2
  • 19
  • 32
0

On blur of your textbox, you'll have to calculate the date difference between what they entered and now().

$('#birthdate').blur(function() {
    $("#ageTextBox").val(getAge(parseDate($('#birthdate').val())));
});

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

    function isLeap(year) {
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }

    // days since the birthdate    
    var days = Math.floor((now.getTime() - birthDate.getTime()) / 1000 / 60 / 60 / 24);
    var age = 0;
    // iterate the years
    for (var y = birthDate.getFullYear(); y <= now.getFullYear(); y++) {
        var daysInYear = isLeap(y) ? 366 : 365;
        if (days >= daysInYear) {
            days -= daysInYear;
            age++;
            // increment the age only if there are available enough days for the year.
        }
    }
    return age;
}
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Avitus
  • 15,640
  • 6
  • 43
  • 53
0

Try the jquery.dates plugin

var d = $("#mytextboxID").dateDiff("unit", date);
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
robert
  • 1,523
  • 5
  • 19
  • 27