NO.1 求中间的日期字符串

作者: 山豆山豆 | 来源:发表于2020-10-13 14:50 被阅读0次

要求 给定另个日期字符串 '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'))

相关文章

网友评论

    本文标题:NO.1 求中间的日期字符串

    本文链接:https://www.haomeiwen.com/subject/woyvpktx.html