js获取当前一周的日期

代码

getWeekTime(){
                // var new_Date = new Date(2021,11,10) //获取指定日期当周的一周日期
                var new_Date = new Date() //获取本周一周日期
                var timesStamp = new_Date.getTime();
                var currenDay = new_Date.getDay();
                var dates = [];
                for(var i = 0; i < 7; i++) {
                    var das = new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7)).toLocaleDateString();
                    das.replace(/[年月]/g, '.').replace(/[日上下午]/g, '');
                    dates.push(das);
                }
                console.log("一周时间:"+JSON.stringify(dates))
            }

结果

从周一到周日

一周时间:["2022/3/21","2022/3/22","2022/3/23","2022/3/24","2022/3/25","2022/3/26","2022/3/27"]

posted @ 2022-03-24 10:01  無玑小姐  阅读(3672)  评论(0编辑  收藏  举报