JavaScript根据日期计算年龄

时间:2015-06-27 08:18:05

标签: javascript date

目前我正在使用JavaScript代码根据用户提供的出生日期计算年龄,其工作正常但显示年龄错误。

mycode:

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;
}
$(".date").on("change", function(){
var dob = $('.date').val();
 var age = getAge(dob); 
 $('.age').val(age);
});

这是jsfiddle Click Here

的链接

如果我给出的日期为21/03/1993 am,请将年龄改为20岁,但正确的年龄为23岁

3 个答案:

答案 0 :(得分:1)

每当我在Javascript中使用日期时,我倾向于使用Moment JS。这使你想要做的事情变得更加简单。

function age(birthDateString){
    var now = moment();
    var birthDate = moment('21/03/1993','DD/MM/YYYY');
    var yearDiff = moment.duration(now - birthDateString).as('years');
    return Math.floor(yearDiff);
}

完成 - age('21/03/1993')返回22

答案 1 :(得分:0)

您的输入格式错误。试试03.21.1993。

答案 2 :(得分:0)

  

日期顺序为mm / dd / yyyy或yyyy / mm / dd

所以,将代码更改为:

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

重要建议:

在javascript日期对象中,月份从0开始,因此您的计算错误。

将代码更改为:

if (monthNow >= monthDob)
  var monthAge = monthNow - monthDob + 1;  //<== add one here
  else {
    yearAge--;
    var monthAge = 12 + monthNow -monthDob;
  }

请参阅此Fiddle