JS两日期相减

JS两日期相减,主要用到下面两个方法

dateObject.setFullYear(year,month,day) 方法

stringObject.split(separator) 方法

function getOffsetDays(startDate, endDate){
   var startDateArr = startDate.split("-");
   var checkStartDate = new Date();
   checkStartDate.setFullYear(startDateArr[0], startDateArr[1], startDateArr[2]);
   var endDateArr = endDate.split("-");
   var checkEndDate = new Date();
   checkEndDate.setFullYear(endDateArr[0], endDateArr[1], endDateArr[2]);
   var days = (checkEndDate.getTime() - checkStartDate.getTime())/ 3600000 / 24; 
   if(startDateArr[0]!=endDateArr[0]){
     flag = true;
   }
   return days;
}

//调用
$(function(){
  var  offsetDays = getOffsetDays("2017-12-01","2017-12-01");
  console.log("相隔天数:"+offsetDays);
));

  

posted @ 2017-12-04 21:28  N神3  阅读(1120)  评论(0编辑  收藏  举报