根据传入的年份和月份获取该月属于本年的第几周和每周的开始和结束日期

function getInfo(year, month) {
       var getInfo = function (year, month) {
                var d = new Date();
                d.setFullYear(year, month - 1, 1);
                var w1 = d.getDay();
                if (w1 == 0) w1 = 7;
                d.setFullYear(year, month, 0);
                var dd = d.getDate();
                if (w1 != 1) {
                    var d1 = 7 - w1 + 2;
                } else {
                    d1 = 1;
                }
                var week_count = Math.ceil((dd - d1 + 1) / 7);
                for (var i = 0; i < week_count; i++) {
                    var monday = d1 + i * 7;
                    var sunday = monday + 6;

             if (monday < 10) {
                        monday = (Array(2).join(0) + monday).slice(-2);
                    }
                    if (sunday < 10) {
                        sunday = (Array(2).join(0) + sunday).slice(-2);
                    }//此处是为了获取的日期中的日如果小10前边的0会自动消失的处理
                    var from = year + "/" + month + "/" + monday;
                    var to;
                    if (sunday <= dd) {
                        to = year + "/" + month + "/" + sunday;
                    } else {
                        d.setFullYear(year, month - 1, sunday);
                        to = d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();
                    }
                    var d11 = new Date(year + "-" + month + "-" + monday);
                    var d2 = new Date(year + "-" + month + "-" + monday);
                    d2.setMonth(0);
                    d2.setDate(1);
                    var rq = d11 - d2;
                    var s1 = Math.ceil(rq / (24 * 60 * 60 * 1000));
                    var s2 = Math.ceil(s1 / 7);
                   console.log("第" + s2 + "周(" + from + " 到 " + to + ")"
                    );
                }
            };
    }
    getInfo('2013','12');

 

posted @ 2016-09-06 09:54  jason.bai  阅读(1523)  评论(0编辑  收藏  举报