获取月份的天数

1.传入日期:2022-03-05

2.获取天数

//获取月份的天数
export function getMonthDays(time) {
    let monthDay = []
    let date = new Date(time)
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let weekIndex = new Date(`${year}/${month}/1`).getDay()
    let BigMonth = [1, 3, 5, 7, 8, 10, 12] //大月 31天
    let allDay = 0
    if(BigMonth.includes(month)) {
        allDay = 31
    } else {
        if(month == 2) {
            if( (year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ){
                allDay = 29
            } else {
                allDay = 28
            }
        } else {
            allDay = 30
        }
    }
    for(let i = 0; i < allDay; i++) {
        let obj = {
            day: i + 1,
            date: `${year}-${addZero(month)}-${addZero(i + 1)}`,
            classList: [],
        }
        monthDay.push(obj)
    }

    if(weekIndex > 0) {
        let blankDay = new Array(weekIndex).fill(0)
        monthDay = [...blankDay, ...monthDay]
    }
    let monthdata = {
        monthDay,
        headTime: `${year}/${addZero(month)}`
    }
    return monthdata
}


版权声明:本文为mmd1234520原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。