首页 > 其他分享 >通过年和周判断这一周的日期

通过年和周判断这一周的日期

时间:2023-02-09 18:13:55浏览次数:37  
标签:判断 一周 Date 日期 3600 time var new day

周日到周六

var week_days=year_week_days(2023, 6);//2023年的第6周
        console.log(week_days,'周日到周六');
        function year_week_days(year, week) {
            var d = new Date(year, 0, 1);
            var time = d.getTime();
            var t = week * 7 * 24 * 3600 * 1000;
            time += t;
            d = new Date(time);
            var w = d.getDay();
            var d1 = new Date(time - (w + 7) * 24 * 3600 * 1000);
            var d2 = new Date(time - (w + 6) * 24 * 3600 * 1000);
            var d3 = new Date(time - (w + 5) * 24 * 3600 * 1000);
            var d4 = new Date(time - (w + 4) * 24 * 3600 * 1000);
            var d5 = new Date(time - (w + 3) * 24 * 3600 * 1000);
            var d6 = new Date(time - (w + 2) * 24 * 3600 * 1000);
            var d7 = new Date(time + ((-1 - w) * 24 * 3600 * 1000));
            return [print_day(d1), print_day(d2), print_day(d3), print_day(d4), print_day(d5), print_day(d6), print_day(d7)]
        };
周一到周日
var week_day=year_week_day(2023, 6);//2023年的第6周
        console.log(week_day,"周一到周日");
        function year_week_day(year, week) {
            var d = new Date(year, 0, 1);
            var time = d.getTime();
            var t = week * 7 * 24 * 3600 * 1000;
            time += t;
            d = new Date(time);
            var w = d.getDay();
            var d1 = new Date(time - (w+6) * 24 * 3600 * 1000);
            var d2 = new Date(time - (w+5) * 24 * 3600 * 1000);
            var d3 = new Date(time - (w+4) * 24 * 3600 * 1000);
            var d4 = new Date(time - (w +3) * 24 * 3600 * 1000);
            var d5 = new Date(time - (w+2) * 24 * 3600 * 1000);
            var d6 = new Date(time - (w+1) * 24 * 3600 * 1000);
            var d7 = new Date(time + ((0-w) * 24 * 3600 * 1000));
            return [print_day(d1),print_day(d2),print_day(d3),print_day(d4),print_day(d5),print_day(d6),print_day(d7)]
        }

公用代码 print_day

function print_day(d) {
            var y = d.getFullYear();
            var m = d.getMonth() + 1;
            m = m < 10 ? "0" + m : "" + m;
            var day = d.getDate();
            day = day < 10 ? "0" + day : "" + day;
            return(y + "-" + m + "-" + day);
        };

 

标签:判断,一周,Date,日期,3600,time,var,new,day
From: https://www.cnblogs.com/zhaofen/p/17106582.html

相关文章