如果给出格式为YYYYMMDD的出生日期,如何以年计算年龄?是否可以使用Date()函数?

我正在寻找一个比我现在使用的更好的解决方案:

Var dob = '19800810'; var年=数字(dob.)substr (0, 4)); var月=数字(dob.)Substr (4, 2)) - 1; var day =数字(dob.)2) substr(6日); var today = new Date(); var age = today.getFullYear() -年份; if (today.getMonth() < month || (today.getMonth() == month && today.getDate() < day)) { 年龄——; } 警报(年龄);


当前回答

我有点晚了,但我发现这是计算出生日期的最简单的方法。

希望这能有所帮助。

function init() { writeYears("myage", 0, Age()); } function Age() { var birthday = new Date(1997, 02, 01), //Year, month-1 , day. today = new Date(), one_year = 1000 * 60 * 60 * 24 * 365; return Math.floor((today.getTime() - birthday.getTime()) / one_year); } function writeYears(id, current, maximum) { document.getElementById(id).innerHTML = current; if (current < maximum) { setTimeout(function() { writeYears(id, ++current, maximum); }, Math.sin(current / maximum) * 200); } } init() <span id="myage"></span>

其他回答

如果年龄只是为了显示(可能不是100%准确),下面的答案是一个很好的方法,但至少它更容易让你理解

function age(birthdate){
  return Math.floor((new Date().getTime() - new Date(birthdate).getTime()) / 3.154e+10)
}

试试这个。

function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}

我相信你的代码中唯一看起来粗糙的是substr部分。

小提琴:http://jsfiddle.net/codeandcloud/n33RJ/

不久前,我做了一个这样的函数:

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

它接受一个Date对象作为输入,所以你需要解析'YYYYMMDD'格式的日期字符串:

var birthDateStr = '19840831',
    parts = birthDateStr.match(/(\d{4})(\d{2})(\d{2})/),
    dateObj = new Date(parts[1], parts[2]-1, parts[3]); // months 0-based!

getAge(dateObj); // 26

替代解决方案,因为为什么不呢:

function calculateAgeInYears (date) {
    var now = new Date();
    var current_year = now.getFullYear();
    var year_diff = current_year - date.getFullYear();
    var birthday_this_year = new Date(current_year, date.getMonth(), date.getDate());
    var has_had_birthday_this_year = (now >= birthday_this_year);

    return has_had_birthday_this_year
        ? year_diff
        : year_diff - 1;
}

我有点晚了,但我发现这是计算出生日期的最简单的方法。

希望这能有所帮助。

function init() { writeYears("myage", 0, Age()); } function Age() { var birthday = new Date(1997, 02, 01), //Year, month-1 , day. today = new Date(), one_year = 1000 * 60 * 60 * 24 * 365; return Math.floor((today.getTime() - birthday.getTime()) / one_year); } function writeYears(id, current, maximum) { document.getElementById(id).innerHTML = current; if (current < maximum) { setTimeout(function() { writeYears(id, ++current, maximum); }, Math.sin(current / maximum) * 200); } } init() <span id="myage"></span>