如何根据出生日期计算年龄

时间:2019-07-23 06:11:07

标签: jquery

如何显示年份和月份?

我需要根据日期选择器中选择的出生日期来计算年龄。

但是当选择某个日期时,显示的年龄不正确。

我已经使用jquery来计算年龄。有时候年龄不正确。

我需要显示x年和x个月大。

当我选择类似22-07-2012的日期显示6岁时。

实际结果是"7 years old"

 dob = new Date($("#DateOfBirth").val())
 var today = new Date();
 var age = Math.floor((today - dob) / (365.25 * 24 * 60 * 60 * 1000));
 $('#txtAge').val(age + ' years old');

4 个答案:

答案 0 :(得分:0)

以下是使用jquery进行年龄计算的示例

function getAge(date) {
    var now = date;
    var bday = document.getElementById('DateOfBirth').value;


    var dateString = bday;

    var yearNow = now.getYear();
    var monthNow = now.getMonth() + 1;
    var dateNow = now.getDate();

    var dob = new Date(dateString.split("-")[2], dateString.split("-")[1] - 1, dateString.split("-")[0]);

    var yearDob = dob.getYear();
    var monthDob = dob.getMonth() + 1;
    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;
    else if ((age.years == 0) && (age.months == 0) && (age.days > 0))
        ageString = age.days + dayString;
    else if ((age.years > 0) && (age.months == 0) && (age.days == 0))
        ageString = age.years + yearString;
    else if ((age.years > 0) && (age.months > 0) && (age.days == 0))
        ageString = age.years + yearString + " , " + age.months + monthString;
    else if ((age.years == 0) && (age.months > 0) && (age.days > 0))
        ageString = age.months + monthString + " , " + age.days + dayString;
    else if ((age.years > 0) && (age.months == 0) && (age.days > 0))
        ageString = age.years + yearString + " , " + age.days + dayString;
    else if ((age.years == 0) && (age.months > 0) && (age.days == 0))
        ageString = age.months + monthString;
    else ageString = "Oops! Could not calculate age!";
    $("#Age").val(ageString);

    return true;
}  

我做什么。

首先获取 DOB 的值,然后将其转换为javascript日期对象,并将其分配给dateString。
我通过今天日期作为日期函数,并将其设置为现在
Now 日期开始获取年,月和日之后。 接下来,我将获得 DOB 的年,月和日。 最后我根据需要检查条件。

答案 1 :(得分:0)

clickLogo(event) {
  console.log(event.detail);
  if (event.detail === 3) {
    console.log('Logo Triple Click!');
  }
}

答案 2 :(得分:0)

可以使用以下代码解决以上问题:

首先将日期存储在变量中,并分隔日,年和月:

var mydate = $("#birth_date").val().toString();
var yearThen = parseInt(mydate.substring(0,4), 10);
var monthThen = parseInt(mydate.substring(5,7), 10);
var dayThen = parseInt(mydate.substring(8,10), 10);

然后获取今天的日期和生日:

var today = new Date();
var birthday = new Date(yearThen, monthThen-1, dayThen);

在那之后,剩下的就是计算了:

var year_age = Math.floor(differenceInMilisecond / 31536000000);
var day_age = Math.floor((differenceInMilisecond % 31536000000) / 86400000);

var month_age = Math.floor(day_age/30);
day_age = day_age % 30;
if (isNaN(year_age) || isNaN(month_age) || isNaN(day_age)) {
        $("#exact_age").text("Invalid birthday - Please try again!");
    }
    else {
        $("#exact_age").html("You are<br/><span id=\"age\">" + year_age + " years " + 
    month_age + " months " + day_age + " days</span> old");
    }

答案 3 :(得分:0)

JavaScript日期对象的格式不为<form> <p> <input type="text" placeholder="First Name" autofocus/> </p> <p> <input type="text" placeholder="Last Name" /> </p> <p> <input type="number" placeholder="Number" /> </p> <p> <input type="text" placeholder="Address" /> </p> <p> <button>Submit</button> </p> </form>,请使用此格式DD-MM-YYYY, 检查下面的代码,它给出了预期的输出。

YYYY-MM-DD
function myFunction() {
  var input = $("#DateOfBirth").val();
  var dob = new Date(input);
  var today = new Date();
  var age = Math.floor((today - dob) / (365.25 * 24 * 60 * 60 * 1000));
  $("#txtAge").html(age + ' years old');

}

我希望这能解决您的问题。