JS获取两个日期的月份差

function getMonthBetween(startDate,endDate){
startDate=new Date(startDate.replace(/-/g,'/'));
endDate=new Date(endDate.replace(/-/g,'/'));
var num=0;
var year=endDate.getFullYear()-startDate.getFullYear();
num+=year*12;
var month=endDate.getMonth()-startDate.getMonth();
num+=month;
var day=endDate.getDate()-startDate.getDate();
if(day>0){
//if(day>15){ num+=1; }
  num+=1;
}else if(day<0){
//if(day<-15){num-=1; }
//num-=1;
}
return num;
}

 

posted on 2016-05-18 17:18  little fat  阅读(2613)  评论(0编辑  收藏  举报