JS根据选择的日期计算年龄

本例中用的是easyUI的datebox

$('#cal_birthday').datebox({
    onSelect: function(date){
        //根据选则的日期计算年龄
        //alert(date.getFullYear()+":"+(date.getMonth()+1)+":"+date.getDate()); // 2017:6:12
        var years = 0;
        var diff = 0;
        d = new Date();
        if(date.getFullYear() < d.getFullYear()){
          years = d.getFullYear() - date.getFullYear();
          if(date.getMonth() > d.getMonth()){
            diff = -1;
          }else if(date.getMonth() == d.getMonth()){
            if(date.getDate() > d.getDate()){diff = -1;}
          }
        }
        $("#cal_age").html(years+diff);
    }
    });

 

posted @ 2017-06-12 17:10  阿龙阿  阅读(2949)  评论(0编辑  收藏  举报