-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathdate-fns-utils.ts
477 lines (397 loc) · 13.4 KB
/
date-fns-utils.ts
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import { addDays } from "date-fns/addDays";
import { addSeconds } from "date-fns/addSeconds";
import { addMinutes } from "date-fns/addMinutes";
import { addHours } from "date-fns/addHours";
import { addWeeks } from "date-fns/addWeeks";
import { addMonths } from "date-fns/addMonths";
import { addYears } from "date-fns/addYears";
import { differenceInYears } from "date-fns/differenceInYears";
import { differenceInQuarters } from "date-fns/differenceInQuarters";
import { differenceInMonths } from "date-fns/differenceInMonths";
import { differenceInWeeks } from "date-fns/differenceInWeeks";
import { differenceInDays } from "date-fns/differenceInDays";
import { differenceInHours } from "date-fns/differenceInHours";
import { differenceInMinutes } from "date-fns/differenceInMinutes";
import { differenceInSeconds } from "date-fns/differenceInSeconds";
import { differenceInMilliseconds } from "date-fns/differenceInMilliseconds";
import { eachDayOfInterval } from "date-fns/eachDayOfInterval";
import { endOfDay } from "date-fns/endOfDay";
import { endOfWeek } from "date-fns/endOfWeek";
import { endOfYear } from "date-fns/endOfYear";
import { format, longFormatters } from "date-fns/format";
import { getDate } from "date-fns/getDate";
import { getDay } from "date-fns/getDay";
import { getDaysInMonth } from "date-fns/getDaysInMonth";
import { getHours } from "date-fns/getHours";
import { getMinutes } from "date-fns/getMinutes";
import { getWeek } from "date-fns/getWeek";
import { getMonth } from "date-fns/getMonth";
import { getSeconds } from "date-fns/getSeconds";
import { getYear } from "date-fns/getYear";
import { isAfter } from "date-fns/isAfter";
import { isBefore } from "date-fns/isBefore";
import { isEqual } from "date-fns/isEqual";
import { isSameDay } from "date-fns/isSameDay";
import { isSameYear } from "date-fns/isSameYear";
import { isSameMonth } from "date-fns/isSameMonth";
import { isSameHour } from "date-fns/isSameHour";
import { isValid } from "date-fns/isValid";
import { parse as dateFnsParse } from "date-fns/parse";
import { setDate } from "date-fns/setDate";
import { setHours } from "date-fns/setHours";
import { setMinutes } from "date-fns/setMinutes";
import { setMonth } from "date-fns/setMonth";
import { setSeconds } from "date-fns/setSeconds";
import { setYear } from "date-fns/setYear";
import { startOfDay } from "date-fns/startOfDay";
import { startOfMonth } from "date-fns/startOfMonth";
import { endOfMonth } from "date-fns/endOfMonth";
import { startOfWeek } from "date-fns/startOfWeek";
import { startOfYear } from "date-fns/startOfYear";
import { parseISO } from "date-fns/parseISO";
import { formatISO } from "date-fns/formatISO";
import { IUtils, DateIOFormats, Unit } from "@date-io/core/IUtils";
import { isWithinInterval } from "date-fns/isWithinInterval";
import { enUS as defaultLocale } from "date-fns/locale/en-US";
type Locale = typeof defaultLocale;
const defaultFormats: DateIOFormats = {
dayOfMonth: "d",
fullDate: "PP",
fullDateWithWeekday: "PPPP",
fullDateTime: "PP p",
fullDateTime12h: "PP hh:mm aa",
fullDateTime24h: "PP HH:mm",
fullTime: "p",
fullTime12h: "hh:mm aa",
fullTime24h: "HH:mm",
hours12h: "hh",
hours24h: "HH",
keyboardDate: "P",
keyboardDateTime: "P p",
keyboardDateTime12h: "P hh:mm aa",
keyboardDateTime24h: "P HH:mm",
minutes: "mm",
month: "LLLL",
monthAndDate: "MMMM d",
monthAndYear: "LLLL yyyy",
monthShort: "MMM",
weekday: "EEEE",
weekdayShort: "EEE",
normalDate: "d MMMM",
normalDateWithWeekday: "EEE, MMM d",
seconds: "ss",
shortDate: "MMM d",
year: "yyyy",
};
export default class DateFnsUtils implements IUtils<Date, Locale> {
public lib = "date-fns";
public locale?: Locale;
public formats: DateIOFormats;
constructor({
locale,
formats,
}: { formats?: Partial<DateIOFormats>; locale?: Locale; instance?: any } = {}) {
this.locale = locale;
this.formats = Object.assign({}, defaultFormats, formats);
}
// Note: date-fns input types are more lenient than this adapter, so we need to expose our more
// strict signature and delegate to the more lenient signature. Otherwise, we have downstream type errors upon usage.
public is12HourCycleInCurrentLocale = () => {
if (this.locale) {
return /a/.test(this.locale.formatLong?.time({}));
}
// By default date-fns is using en-US locale with am/pm enabled
return true;
};
public getFormatHelperText = (format: string) => {
// @see https://github.com/date-fns/date-fns/blob/master/src/format/index.js#L31
const longFormatRegexp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g;
const locale = this.locale || defaultLocale;
return (
format
.match(longFormatRegexp)
?.map((token) => {
const firstCharacter = token[0];
if (firstCharacter === "p" || firstCharacter === "P") {
const longFormatter = longFormatters[firstCharacter];
return longFormatter(token, locale.formatLong);
}
return token;
})
.join("")
.replace(/(aaa|aa|a)/g, "(a|p)m")
.toLocaleLowerCase() ?? format
);
};
public parseISO = (isoString: string) => {
return parseISO(isoString);
};
public toISO = (value: Date) => {
return formatISO(value, { format: "extended" });
};
public getCurrentLocaleCode = () => {
return this.locale?.code || "en-US";
};
public addSeconds = (value: Date, count: number) => {
return addSeconds(value, count);
};
public addMinutes = (value: Date, count: number) => {
return addMinutes(value, count);
};
public addHours = (value: Date, count: number) => {
return addHours(value, count);
};
public addDays = (value: Date, count: number) => {
return addDays(value, count);
};
public addWeeks = (value: Date, count: number) => {
return addWeeks(value, count);
};
public addMonths = (value: Date, count: number) => {
return addMonths(value, count);
};
public addYears = (value: Date, count: number) => {
return addYears(value, count);
};
public isValid = (value: any) => {
return isValid(this.date(value));
};
public getDiff = (value: Date, comparing: Date | string, unit?: Unit) => {
// we output 0 if the compare date is string and parsing is not valid
const dateToCompare = this.date(comparing) ?? value;
if (!this.isValid(dateToCompare)) {
return 0;
}
switch (unit) {
case "years":
return differenceInYears(value, dateToCompare);
case "quarters":
return differenceInQuarters(value, dateToCompare);
case "months":
return differenceInMonths(value, dateToCompare);
case "weeks":
return differenceInWeeks(value, dateToCompare);
case "days":
return differenceInDays(value, dateToCompare);
case "hours":
return differenceInHours(value, dateToCompare);
case "minutes":
return differenceInMinutes(value, dateToCompare);
case "seconds":
return differenceInSeconds(value, dateToCompare);
default: {
return differenceInMilliseconds(value, dateToCompare);
}
}
};
public isAfter = (value: Date, comparing: Date) => {
return isAfter(value, comparing);
};
public isBefore = (value: Date, comparing: Date) => {
return isBefore(value, comparing);
};
public startOfDay = (value: Date) => {
return startOfDay(value);
};
public endOfDay = (value: Date) => {
return endOfDay(value);
};
public getHours = (value: Date) => {
return getHours(value);
};
public setHours = (value: Date, count: number) => {
return setHours(value, count);
};
public setMinutes = (value: Date, count: number) => {
return setMinutes(value, count);
};
public getSeconds = (value: Date) => {
return getSeconds(value);
};
public setSeconds = (value: Date, count: number) => {
return setSeconds(value, count);
};
public isSameDay = (value: Date, comparing: Date) => {
return isSameDay(value, comparing);
};
public isSameMonth = (value: Date, comparing: Date) => {
return isSameMonth(value, comparing);
};
public isSameYear = (value: Date, comparing: Date) => {
return isSameYear(value, comparing);
};
public isSameHour = (value: Date, comparing: Date) => {
return isSameHour(value, comparing);
};
public startOfYear = (value: Date) => {
return startOfYear(value);
};
public endOfYear = (value: Date) => {
return endOfYear(value);
};
public startOfMonth = (value: Date) => {
return startOfMonth(value);
};
public endOfMonth = (value: Date) => {
return endOfMonth(value);
};
public startOfWeek = (value: Date) => {
return startOfWeek(value, { locale: this.locale });
};
public endOfWeek = (value: Date) => {
return endOfWeek(value, { locale: this.locale });
};
public getYear = (value: Date) => {
return getYear(value);
};
public setYear = (value: Date, count: number) => {
return setYear(value, count);
};
date<
TArg extends unknown = undefined,
TRes extends unknown = TArg extends null
? null
: TArg extends undefined
? Date
: Date | null
>(value?: TArg): TRes {
if (typeof value === "undefined") {
return new Date() as TRes;
}
if (value === null) {
return null as TRes;
}
return new Date(value as string | number) as TRes;
}
public toJsDate = (value: Date) => {
return value;
};
public parse = (value: string, formatString: string) => {
if (value === "") {
return null;
}
return dateFnsParse(value, formatString, new Date(), { locale: this.locale });
};
public format = (date: Date, formatKey: keyof DateIOFormats) => {
return this.formatByString(date, this.formats[formatKey]);
};
public formatByString = (date: Date, formatString: string) => {
return format(date, formatString, { locale: this.locale });
};
public isEqual = (date: any, comparing: any) => {
if (date === null && comparing === null) {
return true;
}
return isEqual(date, comparing);
};
public isNull = (date: Date) => {
return date === null;
};
public isAfterDay = (date: Date, value: Date) => {
return isAfter(date, endOfDay(value));
};
public isBeforeDay = (date: Date, value: Date) => {
return isBefore(date, startOfDay(value));
};
public isBeforeYear = (date: Date, value: Date) => {
return isBefore(date, startOfYear(value));
};
public isBeforeMonth(value: Date, comparing: Date): boolean {
return isBefore(value, startOfMonth(comparing));
}
public isAfterMonth(value: Date, comparing: Date): boolean {
return isAfter(value, startOfMonth(comparing));
}
public isAfterYear = (date: Date, value: Date) => {
return isAfter(date, endOfYear(value));
};
public isWithinRange = (date: Date, [start, end]: [Date, Date]) => {
return isWithinInterval(date, { start, end });
};
public formatNumber = (numberToFormat: string) => {
return numberToFormat;
};
public getMinutes = (date: Date) => {
return getMinutes(date);
};
public getDate = (date: Date) => {
return getDate(date);
};
public setDate = (date: Date, count: number) => {
return setDate(date, count);
};
public getWeek = (date: Date) => {
return getWeek(date);
};
public getMonth = (date: Date) => {
return getMonth(date);
};
public getDaysInMonth = (date: Date) => {
return getDaysInMonth(date);
};
public setMonth = (date: Date, count: number) => {
return setMonth(date, count);
};
public getMeridiemText = (ampm: "am" | "pm") => {
return ampm === "am" ? "AM" : "PM";
};
public getNextMonth = (date: Date) => {
return addMonths(date, 1);
};
public getPreviousMonth = (date: Date) => {
return addMonths(date, -1);
};
public getMonthArray = (date: Date) => {
const firstMonth = startOfYear(date);
const monthArray = [firstMonth];
while (monthArray.length < 12) {
const prevMonth = monthArray[monthArray.length - 1];
monthArray.push(this.getNextMonth(prevMonth));
}
return monthArray;
};
public mergeDateAndTime = (date: Date, time: Date) => {
return this.setSeconds(
this.setMinutes(this.setHours(date, this.getHours(time)), this.getMinutes(time)),
this.getSeconds(time)
);
};
public getWeekdays = () => {
const now = new Date();
return eachDayOfInterval({
start: startOfWeek(now, { locale: this.locale }),
end: endOfWeek(now, { locale: this.locale }),
}).map((day) => this.formatByString(day, "EEEEEE"));
};
public getWeekArray = (date: Date) => {
const start = startOfWeek(startOfMonth(date), { locale: this.locale });
const end = endOfWeek(endOfMonth(date), { locale: this.locale });
let count = 0;
let current = start;
const nestedWeeks: Date[][] = [];
let lastDay = null as null | number;
while (isBefore(current, end)) {
const weekNumber = Math.floor(count / 7);
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
const day = getDay(current);
if (lastDay !== day) {
lastDay = day;
nestedWeeks[weekNumber].push(current);
count += 1;
}
current = addDays(current, 1);
}
return nestedWeeks;
};
public getYearRange = (start: Date, end: Date) => {
const startDate = startOfYear(start);
const endDate = endOfYear(end);
const years: Date[] = [];
let current = startDate;
while (isBefore(current, endDate)) {
years.push(current);
current = addYears(current, 1);
}
return years;
};
}