js获取选中日期的当周的周一和周日


js获取选中日期的当周的周一和周日

 第一种方法(推荐):

 1   function getWeekStr(str) {
 2         // 将字符串转为标准时间格式
 3         str2 = Date.parse(str);
 4         let date = new Date(str2);
 5         let month = date.getMonth() + 1;
 6         let week = getWeekFromDate(date);
 7         if (week === 0) {//第0周归于上月的最后一周
 8             month = date.getMonth();
 9             let dateLast = new Date();
10             let dayLast = new Date(dateLast.getFullYear(), dateLast.getMonth(), 0).getDate();
11             let timestamp = new Date(new Date().getFullYear(), new Date().getMonth() - 1, dayLast);
12             week = getWeekFromDate(new Date(timestamp));
13         }
14         let time = month + "月第" + week + "周";
15         return time;
16     }
17 
18     function getWeekFromDate(date) {
19         // 将字符串转为标准时间格式
20         let w = date.getDay();//周几
21         if (w === 0) {
22             w = 7;
23         }
24         let week = Math.ceil((date.getDate() + 6 - w) / 7) - 1;
25         return week;
26     }
27 
28     console.log("2018-02-3---" + getWeekStr("2018-02-3"));
29     console.log("2018-02-4---" + getWeekStr("2018-02-4"));
30     console.log("2018-02-5---" + getWeekStr("2018-02-5"));
31     console.log("2018-02-12---" + getWeekStr("2018-02-12"));
32     console.log("2018-02-19---" + getWeekStr("2018-02-19"));
33     console.log("2018-02-28---" + getWeekStr("2018-02-28"));
34     console.log("2018-03-1---" + getWeekStr("2018-03-1"));
35     console.log("2018-03-5---" + getWeekStr("2018-11-1"));
36     console.log("2018-08-27---" + getWeekStr("2018-12-01"));

 

第二种方法(比较复杂):  

 1 console.log(getNowDateAndNowWeek(1539187200000));
 2     
 3     /**
 4      * 获取当月的第几周
 5      * a = d = 当前日期
 6      * b = 6 - w = 当前周的还有几天过完(不算今天)
 7      * a + b 的和在除以7 就是当天是当前月份的第几周
 8     */
 9     function getMonthWeek(a, b, c) {
10 
11         var date = new Date(a, parseInt(b) - 1, c), w = date.getDay(), d = date.getDate();
12         return Math.ceil(
13             (d + 6 - w) / 7
14         );
15     };
16 
17     
18     /**
19      * 获取选择当前的第几周,当前的周一、周日
20      * time 选择日期的时间戳
21      */
22     function getNowDateAndNowWeek(time) {
23         //选中的时间戳
24         var timestamp = time;
25         var serverDate = new Date(time);
26 
27         //本周周日的的时间
28         var sundayTiem = timestamp + ((7 - serverDate.getDay()) * 24 * 60 * 60 * 1000)
29         var SundayData = new Date(sundayTiem);
30         //
31         var tomorrowY = SundayData.getFullYear();
32         //
33         var tomorrowM = (SundayData.getMonth() + 1 < 10 ? '0' + (SundayData.getMonth() + 1) : SundayData.getMonth() + 1);
34         //
35         var tomorrowD = SundayData.getDate() < 10 ? '0' + SundayData.getDate() : SundayData.getDate();
36         console.log('周日:  ' + tomorrowY + '-' + tomorrowM + '-' + tomorrowD);
37 
38         // 本周周一的时间
39         var mondayTime = timestamp - ((serverDate.getDay() - 1) * 24 * 60 * 60 * 1000)
40         var mondayData = new Date(mondayTime);
41         //
42         var mondayY = mondayData.getFullYear();
43         //
44         var mondayM = (mondayData.getMonth() + 1 < 10 ? '0' + (mondayData.getMonth() + 1) : mondayData.getMonth() + 1);
45         //
46         var mondayD = mondayData.getDate() < 10 ? '0' + mondayData.getDate() : mondayData.getDate();
47         var nowWeek = getMonthWeek(tomorrowY, tomorrowM, tomorrowD);
48         //输出值
49         var config = {
50             SunDay: tomorrowY + '/' + tomorrowM + '/' + tomorrowD,
51             Monday: mondayY + '/' + mondayM + '/' + mondayD,
52             nowWeek: nowWeek
53         }
54         return config;
55     }

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM