JS 遍历本月所有日期

代码如下:

javascript 复制代码
function getMonthAllDays() {
    // 获取当前日期
    var currentDate = new Date();       
    // 获取当前月份的第一天
    var firstDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);          
    // 获取当前月份的最后一天
    var lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);          
    // 遍历当月的所有日期
    let result = "";
    for (var date = firstDayOfMonth; date <= lastDayOfMonth; date.setDate(date.getDate() + 1)) {
      result = result + '<p>' + getJsMonthDay(date) + " " + getWeekday(date) +  '</p>'
      console.log(date.getDate());
    }
    return result;
}

执行效果

相关代码

javascript 复制代码
function dateFtt(fmt,date) {

    var o = {
        "M+" : date.getMonth()+1,                 //月份
        "d+" : date.getDate(),                    //日
        "h+" : date.getHours(),                   //小时
        "m+" : date.getMinutes(),                 //分
        "s+" : date.getSeconds(),                 //秒
        "q+" : Math.floor((date.getMonth()+3)/3), //季度
        "S"  : date.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt))
        fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
        if(new RegExp("("+ k +")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
    return fmt;
}



function getJsMonthDay(dateStr) {

    if ((dateStr == null) ||(dateStr == ""))
    {
        return "";
    } else {
        var date = new Date(dateStr);
        return dateFtt("MM-dd",date);
    }
}


 function getWeekday(date) {
    const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    const weekday = new Date(date).getDay();
    return weekdays[weekday];
}
相关推荐
lly2024061 分钟前
Bootstrap 警告框
开发语言
2601_9491465336 分钟前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧41 分钟前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
passerby60611 小时前
完成前端时间处理的另一块版图
前端·github·web components
KYGALYX1 小时前
服务异步通信
开发语言·后端·微服务·ruby
掘了1 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
zmzb01031 小时前
C++课后习题训练记录Day98
开发语言·c++
崔庆才丨静觅1 小时前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅1 小时前
5分钟快速搭建 AI 平台并用它赚钱!
前端
猫头虎2 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven