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