js根据周次获取周次的日期范围

项目统计中遇到:mysql 获取 当年年周次。到前端来用js转换成日期区间

function getXDate(year,weeks){

        var date = newDate(year,"0","1");

        var time = date.getTime();

        // 获取当前星期几,0:星期一 。。。。

        var _week = date.getDay();

        //当这一年的1月1日为周日时则本年有54周,否则没有54周,没有则去除第54周的提示

        if(_week!=0){//一年53周情况

                    if(weeks==54){

                        return'今年没有54周';

                    }

                    var cnt=0;// 获取距离周末的天数

                    if(_week==0){

                        cnt = 7;

                    }elseif(_week==1){

                        cnt = 6;

                    }elseif(_week==2){

                        cnt = 5;

                    }elseif(_week==3){

                        cnt = 4;

                    }elseif(_week==4){

                        cnt = 3;

                    }elseif(_week==5){

                        cnt = 2;

                    }elseif(_week==6){

                        cnt = 1;

                    }

                    cnt += 1;//加1表示以星期一为一周的第一天

                    // 将这个长整形时间加上第N周的时间偏移

                    time += cnt*24*3600000; //第2周开始时间


                    var nextYear = newDate(parseInt(year,10)+1,"0","1");

                    var nextWeek = nextYear.getDay();

                    var lastcnt = 0;//获取最后一周开始时间到周末的天数

                    if(nextWeek==0){

                        lastcnt = 6;

                    }elseif(nextWeek==1){

                        lastcnt = 0;

                    }elseif(nextWeek==2){

                        lastcnt = 1;

                    }elseif(nextWeek==3){

                        lastcnt = 2;

                    }elseif(nextWeek==4){

                        lastcnt = 3;

                    }elseif(nextWeek==5){

                        lastcnt = 4;

                    }elseif(nextWeek==6){

                        lastcnt = 5;

                    }

                    if(weeks==1){//第1周特殊处理

                        // 为日期对象 date 重新设置成时间 time

                        var start = date.format("yyyy年MM月dd日");

                        date.setTime(time-24*3600000);

                        returnstart +'--'+ date.format("yyyy年MM月dd日");

                    }elseif(weeks==53){//第53周特殊处理

                        var start = time+(weeks-2)*7*24*3600000; //第53周开始时间

                        var end = time+(weeks-2)*7*24*3600000+ lastcnt*24*3600000- 24*3600000; //第53周结束时间

                        // 为日期对象 date 重新设置成时间 time

                        date.setTime(start);

                        var _start = date.format("yyyy年MM月dd日");

                        date.setTime(end);

                        var _end = date.format("yyyy年MM月dd日");

                        return_start +'--'+ _end;

                    }else{

                        var start = time+(weeks-2)*7*24*3600000; //第n周开始时间

                        var end = time+(weeks-1)*7*24*3600000- 24*3600000; //第n周结束时间

                        // 为日期对象 date 重新设置成时间 time

                        date.setTime(start);

                        var _start = date.format("yyyy年MM月dd日");

                        date.setTime(end);

                        var _end = date.format("yyyy年MM月dd日");

                        return_start +'--'+ _end;

                    }

        }else{//一年54周情况

                    var cnt=0;// 获取距离周末的天数

                    if(_week==0&& weeks==1){//第一周

                        cnt = 0;

                    }elseif(_week==0){

                        cnt = 7;

                    }elseif(_week==1){

                        cnt = 6;

                    }elseif(_week==2){

                        cnt = 5;

                    }elseif(_week==3){

                        cnt = 4;

                    }elseif(_week==4){

                        cnt = 3;

                    }elseif(_week==5){

                        cnt = 2;

                    }elseif(_week==6){

                        cnt = 1;

                    }

                    cnt += 1;//加1表示以星期一为一周的第一天

                    // 将这个长整形时间加上第N周的时间偏移

                    time += 24*3600000; //第2周开始时间


                    var nextYear = newDate(parseInt(year,10)+1,"0","1");

                    var nextWeek = nextYear.getDay();

                    var lastcnt = 0;//获取最后一周开始时间到周末的天数

                    if(nextWeek==0){

                        lastcnt = 6;

                    }elseif(nextWeek==1){

                        lastcnt = 0;

                    }elseif(nextWeek==2){

                        lastcnt = 1;

                    }elseif(nextWeek==3){

                        lastcnt = 2;

                    }elseif(nextWeek==4){

                        lastcnt = 3;

                    }elseif(nextWeek==5){

                        lastcnt = 4;

                    }elseif(nextWeek==6){

                        lastcnt = 5;

                    }


                    if(weeks==1){//第1周特殊处理

                        // 为日期对象 date 重新设置成时间 time

                        var start = date.format("yyyy年MM月dd日");

                        date.setTime(time-24*3600000);

                        returnstart +'--'+ date.format("yyyy年MM月dd日");

                    }elseif(weeks==54){//第54周特殊处理

                        var start = time+(weeks-2)*7*24*3600000; //第54周开始时间

                        var end = time+(weeks-2)*7*24*3600000+ lastcnt*24*3600000- 24*3600000; //第53周结束时间

                        // 为日期对象 date 重新设置成时间 time

                        date.setTime(start);

                        var _start = date.format("yyyy年MM月dd日");

                        date.setTime(end);

                        var _end = date.format("yyyy年MM月dd日");

                        return_start +'--'+ _end;

                    }else{

                        var start = time+(weeks-2)*7*24*3600000; //第n周开始时间

                        var end = time+(weeks-1)*7*24*3600000- 24*3600000; //第n周结束时间

                        // 为日期对象 date 重新设置成时间 time

                        date.setTime(start);

                        var _start = date.format("yyyy年MM月dd日");

                        date.setTime(end);

                        var _end = date.format("yyyy年MM月dd日");

                        return_start +'--'+ _end;

                    }

        }

}


附:dataformat:


Date.prototype.format = function (fmt) {

    var o = {

        "M+": this.getMonth() + 1, //月份

        "d+": this.getDate(), //日

        "h+": this.getHours(), //小时

        "m+": this.getMinutes(), //分

        "s+": this.getSeconds(), //秒

        "q+": Math.floor((this.getMonth() + 3) / 3), //季度

        "S": this.getMilliseconds() //毫秒

    };

    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.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;

}

你可能感兴趣的:(js根据周次获取周次的日期范围)