js获取本月第几周和本年第几周

  var getMonthWeek = function (a, b, c) {
    /*
    a = d = 当前日期
    b = 6 - w = 当前周的还有几天过完(不算今天)
    a + b 的和在除以7 就是当天是当前月份的第几周
    */
    var date = new Date(a, parseInt(b) - 1, c), w = date.getDay(), d = date.getDate();
    return Math.ceil(
    (d + 6 - w) / 7
    );
    };
    var getYearWeek = function (a, b, c) {
    /*
    date1是当前日期
    date2是当年第一天
    d是当前日期是今年第多少天
    用d + 当前年的第一天的周差距的和在除以7就是本年第几周
    */
    var date1 = new Date(a, parseInt(b) - 1, c), date2 = new Date(a, 0, 1),
    d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000);
    return Math.ceil(
    (d + ((date2.getDay() + 1) - 1)) / 7
    );
    };
    document.write(
    "今天是本月的第 ", getMonthWeek(2007, 03, 19), ""
    , "今天是本年的第 ", getYearWeek(2007, 03, 19), " 周"
    );
原文地址:https://www.cnblogs.com/siyunianhua/p/4936411.html