jQuery jquery基于dob计算年龄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5925068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:01:37  来源:igfitidea点击:

jquery calculating age based on dob

jqueryhtml

提问by Omnipresent

I have a textfieldwhere the user enters the date of birth in mm/dd/yyyyformat. I want to autofill the agefield for the user based on their entered dob.

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

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

textfield当用户跳出 dob 字段时,应填充年龄。

what is the jQuery way of doing this?

这样做的jQuery方式是什么?

采纳答案by Omnipresent

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

有关计算适当处理闰年的年龄的函数,请参阅在 JavaScript中计算年龄。

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

回答by Avitus

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

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

回答by robert

Try the jquery.dates plugin

试试jquery.dates 插件

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

回答by Demetrius T. Easley

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

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

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

});

回答by Jagathpathi Kristipati

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

回答by Francisco Moreno

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...

晚了,但这有效......