현재 시간을 바탕으로 간편하게 획득할 수 있는 시간(최근 2일, 최근 1주일, 최근 2주일, 최근 1월, 최근 2월, 최근 반년, 최근 1년, 금주, 이달, 금년)

11149 단어
 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             }

좋은 웹페이지 즐겨찾기