Jquery年龄计算日期

时间:2011-04-02 17:37:06

标签: jquery

我是否遗漏了以下jQuery代码中的内容?

var dob = $('#date').val();
if(dob != ''){
    var today = new Date();
    var dayDiff = Math.ceil(today - dob) / (1000 * 60 * 60 * 24 * 365);
    var age = parseInt(dayDiff);
    $('#age').html(age+' years old');
}

我从MySQL db获得#date的预取值。

<input type="text" value="1988-04-07" id="#date" name="dob" /><p id="age"></p>

它正在返回NaN,而不是正确的值。

7 个答案:

答案 0 :(得分:48)

$('#date').val()返回字符串'1988-04-07'。您需要将其解析为实际数字。

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

正如@esqew指出的那样,您还需要将id="#date"更改为id="date"

答案 1 :(得分:2)

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();
 //date must be mm/dd/yyyy
  var dob = new Date(dateString.substring(6,10),
                     dateString.substring(0,2)-1,                   
                     dateString.substring(3,5)                  
                     );

  var yearDob = dob.getFullYear();
  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;
}

// A bit of jQuery to call the getAge() function and update the page...
$(document).ready(function() {
  $("#submitDate").click(function(e) {
    e.preventDefault();

    $("#age").html(getAge($("input#date").val()));

  });
});

and HTML IS

答案 2 :(得分:1)

首先,文本框中提到的ID不应包含“#”

我还创建了一个fiddle

糟糕,我没有查看发布日期

答案 3 :(得分:0)

$("#dob").change(function(){

    var today = new Date();
    var birthDate = new Date($('#dob').val());
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
   return $('#age').html(age+' years old');
});

它对我有用

答案 4 :(得分:0)

 jQuery("#dob").on('change',function(){
       var dob1 = jQuery(this).val();
       var dob = $.datepicker.formatDate('yy-mm-dd', new Date(dob1));
       var str = dob.split('-');    
       var firstdate=new Date(str[0],str[1],str[2]);
       var today = new Date();        
       var dayDiff = Math.ceil(today.getTime() - firstdate.getTime()) / (1000 * 60 * 60 * 24 * 365);
       var age = parseInt(dayDiff);
       jQuery("#age").html(age+' years old');
   });




<input type="text" id="#dob" class="datepicker" name="dob" /><p id="age"></p>

答案 5 :(得分:0)

这就是对我有用的方法,而其余的全都给了NaN:

仅当dob格式为 mm / dd / yy

时有效
function getAge(dob) { return ~~((new Date()-new Date(dob))/(31556952000)) }
$("#dob").val()
$(dob).change(function(){
  $('.age').val(getAge($(this).val()));
});

请参阅我的jsfiddle http://jsfiddle.net/Adam_888/98c21nbz/1/

答案 6 :(得分:0)

我们可以通过使用moment.min.js来计算年龄。 在HTML文件中,请添加以下行

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script>

Js代码:

var dob = $('#date').val();
if(dob != ''){
    var age = moment().diff(moment(dob, 'YYYY-MM-DD'), 'years');
    $('#age').html(age+' years old');
}

希望这会对您有所帮助。