要求 给定另个日期字符串 '2018-06','2018-12'
返回中间的月份日期 数组 [ '2018-7', '2018-8', '2018-9', '2018-10', '2018-11' ]
function arrdate(date1,date2){
let res=[];
let startTime=getDate(date1)
let endTime=getDate(date2)
if(endTime<startTime){
return []
}
while(startTime<endTime){
let date=new Date(startTime);
let year=date.getFullYear();
let nextMonth=date.getMonth()+1;
let nextTime=new Date(year,nextMonth).getTime()
if(nextTime<endTime){
res.push(getDateStr(nextTime))
startTime=nextTime
}else{
break
}
}
return res
}
function getDate(dateStr){
let [year,month]=dateStr.split('-');
let curTime=new Date(year,month-1).getTime()
return curTime
}
function getDateStr(time){
let date=new Date(time)
let year=date.getFullYear();
let month=date.getMonth()+1;
return `${year}-${month}`
}
console.log(arrdate('2018-06','2018-12'))