JS 获取当前月份的所有日期集合(yyyy-MM-dd)


/**
 * 获取当月的日期集合(yyyy-MM-dd)
 */
function currentMonthDays() {
    // 获取标准时间
    const date = new Date();
    // 获取当天日期
    const currentDay = date.getDate();
    // 获取当前月份(实际月份需要加1)
    const currentMonth = date.getMonth() + 1 < 10 ? '0' + date.getMonth() + 1 : date.getMonth() + 1;
    // 获取当前年份
    const currentYear = date.getFullYear();
    // 获取当前月有多少天
    const currentMonthDays = new Date(currentYear, currentMonth, 0).getDate();
    // 当前月份所有日期集合
    const currentMonthArr = [];
    for (let day = 1; day <= currentMonthDays; day++) {
        // 截至当前日期为止
        if (day <= currentDay) {
            // 年月日(yyyy-MM-dd)
            let dateItem = currentYear + "-" + currentMonth + "-" + (day < 10 ? '0' + day : day)
            currentMonthArr.push(dateItem)
        }
    }
    return currentMonthArr;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM