两个年月日相减,获取年数和年数及半年数

很用心的再写,回得到6岁或者6.5岁的样式

 直接放代码:

function workingDate(enroll_date){
  var year=new Date().getFullYear()-enroll_date.split('-')[0];
  // var year=2018-enroll_date.split('-')[0];
  console.log(year);
  var mounth=enroll_date.split('-')[1]-new Date().getMonth()-1;
  // var mounth=enroll_date.split('-')[1]-7-1;
  console.log(mounth);
  var day=enroll_date.split('-')[2]-new Date().getDate();
  // var day=enroll_date.split('-')[2]-20;
  console.log(day);
  if(mounth>=0){
    if (mounth > 6 && day >= 0) {
      year = year - 1;
    }else if (mounth == 6 && day == 0) {
      year = year - 0.5;
    }else if (mounth == 0 && day <= 0) {
      year = year;
    }else {
      year = year - 0.5;
    }
  }else{
    if(mounth<-6){
      year = year + 0.5;
    }else if(mounth==-6 && day==0){
      year = year;
    }else if(mounth>-6){
      year=year;
    }
  }
            return year.toFixed(1);
}
alert(workingDate('2010-01-24'));

				  

  注释:同时 满足了传入的月份大于当前月份或者小于当前时间月份,注释部分是自己测试的传入的月份小于当前时间月份

 

转载于:https://www.cnblogs.com/weiyunblogs/p/10310342.html

你可能感兴趣的:(两个年月日相减,获取年数和年数及半年数)