SOURCE

console 命令行工具 X clear

                    
>
console
function jsGetAge(){ 
  //定义结果变量
  var returnAge;
  //获取页面输入的生日,例如 2010-06-01
  var strBirthday=document.getElementById("year").value;
  //把生日拆分成年、月、日
  var strBirthdayArr=strBirthday.split("-");
  var birthYear = strBirthdayArr[0];
  var birthMonth = strBirthdayArr[1];
  var birthDay = strBirthdayArr[2];
  //从系统获取当前日期
  d = new Date();
  var nowYear = d.getFullYear();
  //月份加1
  var nowMonth = d.getMonth() + 1;
  var nowDay = d.getDate();
  //开始计算年龄
  if(nowYear == birthYear){
  		returnAge = 0;//同年 则为0岁
  }
  else{
      var ageDiff = nowYear - birthYear ; //年之差
      if(ageDiff > 0){
          if(nowMonth == birthMonth) {
          		var dayDiff = nowDay - birthDay;//日之差
              if(dayDiff < 0)
              {
              	returnAge = ageDiff - 1;
              }
              else
              {
             		returnAge = ageDiff ;
              }
          }
          else
          {
              var monthDiff = nowMonth - birthMonth;//月之差
              if(monthDiff < 0)
              {
              	returnAge = ageDiff - 1;
              }
              else
              {
              	returnAge = ageDiff ;
              }
          }
      }
      else
      {
	      returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
      }
  }
 //设置结果输入框的值 
  document.getElementById("end").value=returnAge;
}
<body>
<div class="top" style="height:70px;border-bottom:1px solid #ddd;">
<img class="logoIndex" height="60" src="https://www.zenitour.com/img/logoIndex.png" style="margin-left:10px;margin-top:5px;">
</div>
<div class="content" style="text-align:center;margin-top:50px;height: 78%;">
<h2>根据生日计算年龄</h2>
<input type='date' id='year' /> 
<input type='button' value=' 计算' onclick="jsGetAge()"/>  
<input type='text' id='end' />    
</div>
<div id="bottom" style="text-align:center;border-top:1px solid #ddd;">
<span class="centerspan" style="font-size:10px">Copyright © 2017 Zenitour lnc All Rights Reserved 版权所有-哲途教育  渝ICP备13002955号-2</span>
</div>
</body>
   html,body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
        }