js 判断第几周

耗尽温柔 提交于 2020-03-11 03:39:21


         //周一为一周的开始,第一周从第一个周一开始算

        //2011.01.03为2011年周一的开始
        function GetWeekIndex(dateobj) {
            var firstDay = GetFirstWeekBegDay(dateobj.getFullYear());
            if (dateobj < firstDay) {
                firstDay = GetFirstWeekBegDay(dateobj.getFullYear() - 1);
            }
            d = Math.floor((dateobj.valueOf() - firstDay.valueOf()) / 86400000);
            //document.write(dateobj.getYear() + "/" + (dateobj.getMonth()+1) + "/" + dateobj.getDate()  + " 第" + (Math.floor(d / 7) + 1) + "周"+"<br/>");
            return Math.floor(d / 7) + 1;
        }
        function GetFirstWeekBegDay(year) {
            var tempdate = new Date(year, 0, 1);
            var temp = tempdate.getDay();
            if (temp == 1)
                return tempdate;
            temp = temp == 0 ? 7 : temp;
            tempdate = tempdate.setDate(tempdate.getDate() + (8 - temp));
            return new Date(tempdate);
        }
        GetWeekIndex(new Date(2007, 0, 1));
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!