|
@@ -1,127 +1,48 @@
|
1
|
|
-import { Injectable } from '@angular/core';
|
|
1
|
+import { Injectable } from "@angular/core";
|
|
2
|
+import {
|
|
3
|
+ addDays,
|
|
4
|
+ startOfWeek,
|
|
5
|
+ endOfMonth,
|
|
6
|
+ startOfDay,
|
|
7
|
+ addMonths,
|
|
8
|
+ startOfMonth,
|
|
9
|
+ startOfYear,
|
|
10
|
+ addYears,
|
|
11
|
+ endOfYear,
|
|
12
|
+} from "date-fns";
|
2
|
13
|
|
3
|
14
|
@Injectable({
|
4
|
|
- providedIn: 'root'
|
|
15
|
+ providedIn: "root",
|
5
|
16
|
})
|
6
|
17
|
export class DateService {
|
7
|
|
-
|
8
|
|
- constructor() { }
|
|
18
|
+ constructor() {}
|
|
19
|
+ /**
|
|
20
|
+ * 日期操作
|
|
21
|
+ */
|
9
|
22
|
date() {
|
10
|
|
- /**
|
11
|
|
- * 获取本周、本季度、本月、上月的开始日期、结束日期
|
12
|
|
- */
|
13
|
|
- var now = new Date(); //当前日期
|
14
|
|
- var nowDayOfWeek = now.getDay(); //今天本周的第几天
|
15
|
|
- var nowDay = now.getDate(); //当前日
|
16
|
|
- var nowMonth = now.getMonth(); //当前月
|
17
|
|
- var nowYear = now.getFullYear(); //当前年
|
18
|
|
- nowYear += (nowYear < 2000) ? 1900 : 0; //
|
19
|
|
- var lastMonthDate = new Date(); //上月日期
|
20
|
|
- lastMonthDate.setDate(1);
|
21
|
|
- lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
|
22
|
|
- var lastYear = lastMonthDate.getFullYear();
|
23
|
|
- var lastMonth = lastMonthDate.getMonth();
|
24
|
|
- var lastMonthYear = lastMonthDate.getFullYear();//上月的年份
|
25
|
|
- //格式化日期:yyyy-MM-dd
|
26
|
|
- function formatDate(date) {
|
27
|
|
- var myyear = date.getFullYear();
|
28
|
|
- var mymonth = date.getMonth() + 1;
|
29
|
|
- var myweekday = date.getDate();
|
30
|
|
- if (mymonth < 10) {
|
31
|
|
- mymonth = "0" + mymonth;
|
32
|
|
- }
|
33
|
|
- if (myweekday < 10) {
|
34
|
|
- myweekday = "0" + myweekday;
|
35
|
|
- }
|
36
|
|
- return (myyear + "-" + mymonth + "-" + myweekday);
|
37
|
|
- }
|
38
|
|
- //获得某月的天数
|
39
|
|
- function getMonthDays(myMonth) {
|
40
|
|
- var monthStartDate = new Date(nowYear, myMonth, 1);
|
41
|
|
- var monthEndDate = new Date(nowYear, myMonth + 1, 1);
|
42
|
|
- var days = (Number(monthEndDate) - Number(monthStartDate)) / (1000 * 60 * 60 * 24);
|
43
|
|
- return days;
|
44
|
|
- }
|
45
|
|
- //获得本季度的开始月份
|
46
|
|
- function getQuarterStartMonth() {
|
47
|
|
- var quarterStartMonth = 0;
|
48
|
|
- if (nowMonth < 3) {
|
49
|
|
- quarterStartMonth = 0;
|
50
|
|
- }
|
51
|
|
- if (2 < nowMonth && nowMonth < 6) {
|
52
|
|
- quarterStartMonth = 3;
|
53
|
|
- }
|
54
|
|
- if (5 < nowMonth && nowMonth < 9) {
|
55
|
|
- quarterStartMonth = 6;
|
56
|
|
- }
|
57
|
|
- if (nowMonth > 8) {
|
58
|
|
- quarterStartMonth = 9;
|
59
|
|
- }
|
60
|
|
- return quarterStartMonth;
|
61
|
|
- }
|
62
|
|
- //获得本周的开始日期
|
63
|
|
- function getWeekStartDate() {
|
64
|
|
- var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
|
65
|
|
- return weekStartDate;
|
66
|
|
- }
|
67
|
|
- //获得本周的结束日期
|
68
|
|
- function getWeekEndDate() {
|
69
|
|
- var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
|
70
|
|
- return weekEndDate;
|
71
|
|
- }
|
72
|
|
- //获得上周的开始日期
|
|
23
|
+ //获得上周的开始日期(上周一 00:00:00)
|
73
|
24
|
function getLastWeekStartDate() {
|
74
|
|
- var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6);
|
75
|
|
- // var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7);//周日为一周的第一天
|
76
|
|
- return weekStartDate;
|
|
25
|
+ return addDays(startOfWeek(new Date()), -6);
|
77
|
26
|
}
|
78
|
|
- //获得上周的结束日期
|
|
27
|
+ //获得上周的结束日期(上周日 00:00:00)
|
79
|
28
|
function getLastWeekEndDate() {
|
80
|
|
- var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
|
81
|
|
- // var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek-1);//周六为一周的最后一天
|
82
|
|
- return weekEndDate;
|
|
29
|
+ return startOfWeek(new Date());
|
83
|
30
|
}
|
84
|
|
- //获得本月的开始日期
|
85
|
|
- function getMonthStartDate() {
|
86
|
|
- var monthStartDate = new Date(nowYear, nowMonth, 1);
|
87
|
|
- return monthStartDate;
|
88
|
|
- }
|
89
|
|
- //获得本月的结束日期
|
90
|
|
- function getMonthEndDate() {
|
91
|
|
- var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
|
92
|
|
- return monthEndDate;
|
93
|
|
- }
|
94
|
|
- //获得上月开始时间
|
|
31
|
+ //获得上月开始时间(上月1号 00:00:00)
|
95
|
32
|
function getLastMonthStartDate() {
|
96
|
|
- var lastMonthStartDate = new Date(lastMonthYear, lastMonth, 1);
|
97
|
|
- return lastMonthStartDate;
|
|
33
|
+ return startOfMonth(addMonths(new Date(), -1));
|
98
|
34
|
}
|
99
|
|
- //获得上月结束时间
|
|
35
|
+ //获得上月结束时间(上月底 00:00:00)
|
100
|
36
|
function getLastMonthEndDate() {
|
101
|
|
- var lastMonthEndDate = new Date(lastMonthYear, lastMonth, getMonthDays(lastMonth));
|
102
|
|
- return lastMonthEndDate;
|
103
|
|
- }
|
104
|
|
- //获得本季度的开始日期
|
105
|
|
- function getQuarterStartDate() {
|
106
|
|
- var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
|
107
|
|
- return quarterStartDate;
|
|
37
|
+ return startOfDay(endOfMonth(addMonths(new Date(), -1)));
|
108
|
38
|
}
|
109
|
|
- //获得本季度的结束日期
|
110
|
|
- function getQuarterEndDate() {
|
111
|
|
- var quarterEndMonth = getQuarterStartMonth() + 2;
|
112
|
|
- var quarterStartDate = new Date(nowYear, quarterEndMonth,
|
113
|
|
- getMonthDays(quarterEndMonth));
|
114
|
|
- return quarterStartDate;
|
115
|
|
- }
|
116
|
|
- //获得上年的开始日期
|
|
39
|
+ //获得上年的开始日期(去年1月1日 00:00:00)
|
117
|
40
|
function getLastYearStartDate() {
|
118
|
|
- var yearStartDate = new Date(nowYear - 1, 0, 1);
|
119
|
|
- return yearStartDate;
|
|
41
|
+ return startOfYear(addYears(new Date(), -1));
|
120
|
42
|
}
|
121
|
43
|
//获得上年的结束日期
|
122
|
44
|
function getLastYearEndDate() {
|
123
|
|
- var yearEndDate = new Date(nowYear - 1, 11, 31);
|
124
|
|
- return yearEndDate;
|
|
45
|
+ return startOfDay(endOfYear(addYears(new Date(), -1)));
|
125
|
46
|
}
|
126
|
47
|
return {
|
127
|
48
|
lastWeekStartDate: getLastWeekStartDate(),
|
|
@@ -130,7 +51,6 @@ export class DateService {
|
130
|
51
|
lastMonthEndDate: getLastMonthEndDate(),
|
131
|
52
|
lastYearStartDate: getLastYearStartDate(),
|
132
|
53
|
lastYearEndDate: getLastYearEndDate(),
|
133
|
|
- formatDate: formatDate,
|
134
|
|
- }
|
|
54
|
+ };
|
135
|
55
|
}
|
136
|
|
-}
|
|
56
|
+}
|