兩個年月日相減,獲取年數和年數及半年數


很用心的再寫,回得到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'));

				  

  注釋:同時 滿足了傳入的月份大於當前月份或者小於當前時間月份,注釋部分是自己測試的傳入的月份小於當前時間月份

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM