基于dob计算年龄的jquery

时间:2011-05-08 01:24:44

标签: jquery html

我有一个textfield,用户以mm/dd/yyyy格式输入出生日期。我想根据输入的dob为用户自动填充age字段。

当用户标签超出dob字段时,应填充年龄textfield

jQuery的做法是什么?

6 个答案:

答案 0 :(得分:3)

请参阅Calculate age in JavaScript以获取计算适当处理闰年的年龄的函数。

然后您可以使用该函数计算用户的年龄,如下所示:

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

..然后将年龄字段设置为该数字:

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

答案 1 :(得分:0)

对于文本框的模糊,您必须计算他们输入的内容与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;
}

答案 2 :(得分:0)

尝试jquery.dates plugin

var d = $("#mytextboxID").dateDiff("unit", date);

答案 3 :(得分:0)

我想出了一个简单的解决方案。它是一个jQuery change函数,它将值发送到输入年龄字段。

$(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);
    });

});

答案 4 :(得分:0)

这里我们通过开始和结束日期来测试它。您还可以使用它来计算月和日的年龄。

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)");    

答案 5 :(得分: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;
}

迟到但这有效......