编辑代码

/**
 * 根据月份返回当月所有日期
 */
function getDateList(target) {
    // 目标月的年月
    const targetYear = target.split('-')[0], targetMouth = target.split('-')[1]

    // 下一个月的年月
    let year = target.split('-')[0], mouth = target.split('-')[1]
    if (Number(mouth) === 12) {
        year++
        mouth = 1
    } else {
        mouth++
    }

    const nextMouth = `${year}-${mouth}`

    // setDate(n)设置日期的第n天,1就是第一天,那么0就是上个月的最后1天
    // 知道上个月最后1天的日期是几号,自然就知道上个月有多少天了
    const dateCount = new Date(new Date(nextMouth).setDate(0)).getDate()

    const dateList = []
    for (let i = 1; i <= dateCount; i++) {
        dateList.push(`${targetYear}-${targetMouth}-${i}`)
    }

    return dateList
}

console.log(getDateList('2020-2'))