-
Notifications
You must be signed in to change notification settings - Fork 647
Expand file tree
/
Copy pathdate-helper.ts
More file actions
241 lines (228 loc) · 7.1 KB
/
date-helper.ts
File metadata and controls
241 lines (228 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { Task, ViewMode } from "../types/public-types";
import DateTimeFormatOptions = Intl.DateTimeFormatOptions;
import DateTimeFormat = Intl.DateTimeFormat;
type DateHelperScales =
| "year"
| "month"
| "day"
| "hour"
| "minute"
| "second"
| "millisecond";
const intlDTCache = {};
export const getCachedDateTimeFormat = (
locString: string | string[],
opts: DateTimeFormatOptions = {}
): DateTimeFormat => {
const key = JSON.stringify([locString, opts]);
let dtf = intlDTCache[key];
if (!dtf) {
dtf = new Intl.DateTimeFormat(locString, opts);
intlDTCache[key] = dtf;
}
return dtf;
};
export const addToDate = (
date: Date,
quantity: number,
scale: DateHelperScales
) => {
const newDate = new Date(
date.getFullYear() + (scale === "year" ? quantity : 0),
date.getMonth() + (scale === "month" ? quantity : 0),
date.getDate() + (scale === "day" ? quantity : 0),
date.getHours() + (scale === "hour" ? quantity : 0),
date.getMinutes() + (scale === "minute" ? quantity : 0),
date.getSeconds() + (scale === "second" ? quantity : 0),
date.getMilliseconds() + (scale === "millisecond" ? quantity : 0)
);
return newDate;
};
export const startOfDate = (date: Date, scale: DateHelperScales) => {
const scores = [
"millisecond",
"second",
"minute",
"hour",
"day",
"month",
"year",
];
const shouldReset = (_scale: DateHelperScales) => {
const maxScore = scores.indexOf(scale);
return scores.indexOf(_scale) <= maxScore;
};
const newDate = new Date(
date.getFullYear(),
shouldReset("year") ? 0 : date.getMonth(),
shouldReset("month") ? 1 : date.getDate(),
shouldReset("day") ? 0 : date.getHours(),
shouldReset("hour") ? 0 : date.getMinutes(),
shouldReset("minute") ? 0 : date.getSeconds(),
shouldReset("second") ? 0 : date.getMilliseconds()
);
return newDate;
};
export const ganttDateRange = (
tasks: Task[],
viewMode: ViewMode,
preStepsCount: number
) => {
let newStartDate: Date = tasks[0].start;
let newEndDate: Date = tasks[0].start;
for (const task of tasks) {
if (task.start < newStartDate) {
newStartDate = task.start;
}
if (task.end > newEndDate) {
newEndDate = task.end;
}
}
switch (viewMode) {
case ViewMode.Year:
newStartDate = addToDate(newStartDate, -1, "year");
newStartDate = startOfDate(newStartDate, "year");
newEndDate = addToDate(newEndDate, 1, "year");
newEndDate = startOfDate(newEndDate, "year");
break;
case ViewMode.QuarterYear:
newStartDate = addToDate(newStartDate, -3, "month");
newStartDate = startOfDate(newStartDate, "month");
newEndDate = addToDate(newEndDate, 3, "year");
newEndDate = startOfDate(newEndDate, "year");
break;
case ViewMode.Month:
newStartDate = addToDate(newStartDate, -1 * preStepsCount, "month");
newStartDate = startOfDate(newStartDate, "month");
newEndDate = addToDate(newEndDate, 1, "year");
newEndDate = startOfDate(newEndDate, "year");
break;
case ViewMode.Week:
newStartDate = startOfDate(newStartDate, "day");
newStartDate = addToDate(
getMonday(newStartDate),
-7 * preStepsCount,
"day"
);
newEndDate = startOfDate(newEndDate, "day");
newEndDate = addToDate(newEndDate, 2, "month");
break;
case ViewMode.Day:
newStartDate = startOfDate(newStartDate, "day");
newStartDate = addToDate(newStartDate, -1 * preStepsCount, "day");
newEndDate = startOfDate(newEndDate, "day");
newEndDate = addToDate(newEndDate, 19, "day");
break;
case ViewMode.QuarterDay:
newStartDate = startOfDate(newStartDate, "day");
newStartDate = addToDate(newStartDate, -1 * preStepsCount, "day");
newEndDate = startOfDate(newEndDate, "day");
newEndDate = addToDate(newEndDate, 66, "hour"); // 24(1 day)*3 - 6
break;
case ViewMode.HalfDay:
newStartDate = startOfDate(newStartDate, "day");
newStartDate = addToDate(newStartDate, -1 * preStepsCount, "day");
newEndDate = startOfDate(newEndDate, "day");
newEndDate = addToDate(newEndDate, 108, "hour"); // 24(1 day)*5 - 12
break;
case ViewMode.Hour:
newStartDate = startOfDate(newStartDate, "hour");
newStartDate = addToDate(newStartDate, -1 * preStepsCount, "hour");
newEndDate = startOfDate(newEndDate, "day");
newEndDate = addToDate(newEndDate, 1, "day");
break;
}
return [newStartDate, newEndDate];
};
export const seedDates = (
startDate: Date,
endDate: Date,
viewMode: ViewMode
) => {
let currentDate: Date = new Date(startDate);
const dates: Date[] = [currentDate];
while (currentDate < endDate) {
switch (viewMode) {
case ViewMode.Year:
currentDate = addToDate(currentDate, 1, "year");
break;
case ViewMode.QuarterYear:
currentDate = addToDate(currentDate, 3, "month");
break;
case ViewMode.Month:
currentDate = addToDate(currentDate, 1, "month");
break;
case ViewMode.Week:
currentDate = addToDate(currentDate, 7, "day");
break;
case ViewMode.Day:
currentDate = addToDate(currentDate, 1, "day");
break;
case ViewMode.HalfDay:
currentDate = addToDate(currentDate, 12, "hour");
break;
case ViewMode.QuarterDay:
currentDate = addToDate(currentDate, 6, "hour");
break;
case ViewMode.Hour:
currentDate = addToDate(currentDate, 1, "hour");
break;
}
dates.push(currentDate);
}
return dates;
};
export const getLocaleMonth = (date: Date, locale: string) => {
let bottomValue = getCachedDateTimeFormat(locale, {
month: "long",
}).format(date);
bottomValue = bottomValue.replace(
bottomValue[0],
bottomValue[0].toLocaleUpperCase()
);
return bottomValue;
};
export const getLocalDayOfWeek = (
date: Date,
locale: string,
format?: "long" | "short" | "narrow" | undefined
) => {
let bottomValue = getCachedDateTimeFormat(locale, {
weekday: format,
}).format(date);
bottomValue = bottomValue.replace(
bottomValue[0],
bottomValue[0].toLocaleUpperCase()
);
return bottomValue;
};
/**
* Returns monday of current week
* @param date date for modify
*/
const getMonday = (date: Date) => {
const day = date.getDay();
const diff = date.getDate() - day + (day === 0 ? -6 : 1); // adjust when day is sunday
return new Date(date.setDate(diff));
};
export const getWeekNumberISO8601 = (date: Date) => {
const tmpDate = new Date(date.valueOf());
const dayNumber = (tmpDate.getDay() + 6) % 7;
tmpDate.setDate(tmpDate.getDate() - dayNumber + 3);
const firstThursday = tmpDate.valueOf();
tmpDate.setMonth(0, 1);
if (tmpDate.getDay() !== 4) {
tmpDate.setMonth(0, 1 + ((4 - tmpDate.getDay() + 7) % 7));
}
const weekNumber = (
1 + Math.ceil((firstThursday - tmpDate.valueOf()) / 604800000)
).toString();
if (weekNumber.length === 1) {
return `0${weekNumber}`;
} else {
return weekNumber;
}
};
export const getDaysInMonth = (month: number, year: number) => {
return new Date(year, month + 1, 0).getDate();
};