js获取某一个时间的上个月或者上一周的起始点

 1 > 上周
 2 
 3 ```
 4 function getSomeTimesWeekDate(Time = new Date()) {
 5             let weekNum = Time.getDay();    //获取当前是周几
 6             weekNum = weekNum == 0 ? 7 : weekNum;
 7             let lastDate = new Date(Time.getTime() - weekNum * 24 * 60 * 60 * 1000);
 8             let fitstDate = new Date(Time.getTime() - ((weekNum + 6) * 24 * 60 * 60 * 1000));
 9             let startDate =
10                 `${fitstDate.getFullYear()}-${fitstDate.getMonth()+1<10?'0'+(fitstDate.getMonth()+1):fitstDate.getMonth()+1}-${fitstDate.getDate()<10?'0'+fitstDate.getDate():fitstDate.getDate()} 00:00:00`
11             let endDate =
12                 `${lastDate.getFullYear()}-${lastDate.getMonth()+1<10?'0'+(lastDate.getMonth()+1):lastDate.getMonth()+1}-${lastDate.getDate()<10?'0'+lastDate.getDate():lastDate.getDate()} 23:59:59`
13             return [startDate, endDate]
14         }
15 
16 ```
17 
18 
19 
20 
21 
22 > 上个月
23 
24 
25 ```
26 function getSomeTimesMonthDate(Time = new Date()) {
27             let year = Time.getFullYear();
28             let month = Time.getMonth();
29             if (month == 0) {
30                 month = 12;
31                 year = year - 1;
32             }
33             if (month < 10) {
34                 month = '0' + month;
35             }
36             let myDate = new Date(year, month, 0); //上个月的最后一天的开始
37             let startDate = `${year}-${month}-01 00:00:00`;
38             let endDate = `${year}-${month}-${myDate.getDate()} 23:59:59` 
39             return [startDate, endDate]
40         }
41 
42 
43 ```

猜你喜欢

转载自www.cnblogs.com/pingzia/p/12761692.html