以月,日计算年龄

时间:2016-08-02 19:47:25

标签: javascript

我一直在使用以下代码,但在JS方面我是一个完全的初学者。我在帖子ygssoni的代码here中对此进行了调整,并使用Angela的编辑进行更灵活的日期输入。

我会如何修改此信息,以便在出生日期的某个月和几天内报告某人的年龄?

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

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


alert(getAge('09/09/1989'));

2 个答案:

答案 0 :(得分:2)

您基本上应该删除代码后半部分中对“years”的所有引用,并重写您的大型if / else部分,如jsFiddle。我改变的唯一部分是第48和第48行。下:

var totMonths = age.months + 12 * age.years; 
if ( totMonths > 1 ) monthString = " months";
else monthString = " month";
if ( age.days > 1 ) dayString = " days";
else dayString = " day";


if ( (totMonths == 0) && (age.days > 0) ) {
    ageString = "Only " + age.days + dayString + " old!";
} else if ( (totMonths > 0) && (age.days == 0)) {
    ageString = totMonths + monthString + " old.";
} else if ( (totMonths > 0) && (age.days > 0))
   ageString = totMonths + monthString + " and " + age.days + dayString + " old.";
} else ageString = "Oops! Could not calculate age!";

答案 1 :(得分:0)

试试这个:

var dob = new Date("1/1/2016");
    var now = new Date();
    var timeMiliseconds = Math.abs(now.getTime() - dob.getTime());
    var days = Math.ceil(timeMiliseconds / (1000 * 3600 * 24)); 
    var monthsElapsed = Math.ceil(timeMiliseconds / (1000 * 3600 * 24 * 30))
    var daysElapsed = days % 30
    alert(monthsElapsed);
    alert(daysElapsed);