以当前时间为基础,便捷获取时间(最近2天,最近1周,最近2周,最近1月,最近2月,最近半年,最近一年,本周,本月,本年)

 1 console.log(day('最近1周'));        
 2 
 3 function day(newval) {
 4                 const date = new Date();
 5                 let endTime=formatTime(date);
 6                 let date1 = Date.parse(date);
 7                 let start = '';
 8                 let end = '';
 9                 let oneDay = 1000 * 3600 * 24;
10 
11                 switch (newval) {
12                     case '今天':
13                         start = new Date();
14                         break;
15                     case '最近2天':
16                         start = date1 - oneDay * 2;
17                         break;
18                     case '最近1周':
19                         start = date1 - oneDay * 7;
20                         break;
21                     case '最近2周':
22                         start = date1 - oneDay * 14;
23                         break;
24                     case '最近1月':
25                         start = new Date();
26                         start.setMonth(start.getMonth() - 1,)
27                         break;
28                     case '最近2月':
29                         start = new Date();
30                         start.setMonth(start.getMonth() - 2,)
31                         break;
32                     case '最近半年':
33                         start = date1 - oneDay * 183;
34                         break;
35                     case '最近一年':
36                         start = new Date();
37                         start.setYear(start.getFullYear() - 1)
38                         break;
39                     case '本周':
40                         let a = 6 - date.getDay();
41                         start = new Date(date1 - oneDay * a).setHours(0, 0, 0, 0);
42                         end = new Date(date1 + oneDay * (1 + date.getDay())).setHours(24, 0, 0, 0)
43                         break;
44                     case '本月':
45                         start = new Date();
46                         start.setHours(0, 0, 0, 0)
47                         start.setMonth(start.getMonth(), 1)
48                         end = new Date(start)
49                         end.setHours(0, 0, 0, 0)
50                         end.setMonth(start.getMonth() + 1, 1)
51                         break;
52                     case '本年':
53                         start = new Date();
54                         start.setHours(0, 0, 0, 0)
55                         start.setMonth(0, 1)
56                         start.setYear(start.getFullYear())
57                         end = new Date(start)
58                         end.setHours(0, 0, 0, 0)
59                         end.setMonth(start.getMonth(), 1)
60                         end.setYear(start.getFullYear() + 1)
61                         break;
62                 }
63 
64                 return {
65                     startTime:this.formatTime(new Date(start)),
66                     endTime:end ?  this.formatTime(new Date(end)) : endTime ,
67                 }
68 
69             }
70 
71   //格式化时间
72        function     formatTime(param) {
73                 let y = param.getFullYear();
74                 let m = param.getMonth() + 1;
75                 let d = param.getDate();
76                 m = m < 10 ? ("0" + m) : m;
77                 d = d < 10 ? ("0" + d) : d;
78                 return y + "-" + m + "-" + d + " ";
79             }

 

你可能感兴趣的:(以当前时间为基础,便捷获取时间(最近2天,最近1周,最近2周,最近1月,最近2月,最近半年,最近一年,本周,本月,本年))