-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
date_utils.js
767 lines (673 loc) · 20.7 KB
/
date_utils.js
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
import isDate from "date-fns/isDate";
import isValidDate from "date-fns/isValid";
import format from "date-fns/format";
import addMinutes from "date-fns/addMinutes";
import addHours from "date-fns/addHours";
import addDays from "date-fns/addDays";
import addWeeks from "date-fns/addWeeks";
import addMonths from "date-fns/addMonths";
import addYears from "date-fns/addYears";
import subMinutes from "date-fns/subMinutes";
import subHours from "date-fns/subHours";
import subDays from "date-fns/subDays";
import subWeeks from "date-fns/subWeeks";
import subMonths from "date-fns/subMonths";
import subYears from "date-fns/subYears";
import getSeconds from "date-fns/getSeconds";
import getMinutes from "date-fns/getMinutes";
import getHours from "date-fns/getHours";
import getDay from "date-fns/getDay";
import getDate from "date-fns/getDate";
import getISOWeek from "date-fns/getISOWeek";
import getMonth from "date-fns/getMonth";
import getQuarter from "date-fns/getQuarter";
import getYear from "date-fns/getYear";
import getTime from "date-fns/getTime";
import setSeconds from "date-fns/setSeconds";
import setMinutes from "date-fns/setMinutes";
import setHours from "date-fns/setHours";
import setMonth from "date-fns/setMonth";
import setQuarter from "date-fns/setQuarter";
import setYear from "date-fns/setYear";
import min from "date-fns/min";
import max from "date-fns/max";
import differenceInCalendarDays from "date-fns/differenceInCalendarDays";
import differenceInCalendarMonths from "date-fns/differenceInCalendarMonths";
import differenceInCalendarWeeks from "date-fns/differenceInCalendarWeeks";
import differenceInCalendarYears from "date-fns/differenceInCalendarYears";
import startOfDay from "date-fns/startOfDay";
import startOfWeek from "date-fns/startOfWeek";
import startOfMonth from "date-fns/startOfMonth";
import startOfQuarter from "date-fns/startOfQuarter";
import startOfYear from "date-fns/startOfYear";
import endOfDay from "date-fns/endOfDay";
import endOfWeek from "date-fns/endOfWeek";
import endOfMonth from "date-fns/endOfMonth";
import endOfYear from "date-fns/endOfYear";
import dfIsEqual from "date-fns/isEqual";
import dfIsSameDay from "date-fns/isSameDay";
import dfIsSameMonth from "date-fns/isSameMonth";
import dfIsSameYear from "date-fns/isSameYear";
import dfIsSameQuarter from "date-fns/isSameQuarter";
import isAfter from "date-fns/isAfter";
import isBefore from "date-fns/isBefore";
import isWithinInterval from "date-fns/isWithinInterval";
import toDate from "date-fns/toDate";
import parse from "date-fns/parse";
import parseISO from "date-fns/parseISO";
import longFormatters from "date-fns/esm/_lib/format/longFormatters";
export const DEFAULT_YEAR_ITEM_NUMBER = 12;
// This RegExp catches symbols escaped by quotes, and also
// sequences of symbols P, p, and the combinations like `PPPPPPPppppp`
var longFormattingTokensRegExp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g;
// ** Date Constructors **
export function newDate(value) {
const d = value
? typeof value === "string" || value instanceof String
? parseISO(value)
: toDate(value)
: new Date();
return isValid(d) ? d : null;
}
export function parseDate(value, dateFormat, locale, strictParsing, minDate) {
let parsedDate = null;
let localeObject =
getLocaleObject(locale) || getLocaleObject(getDefaultLocale());
let strictParsingValueMatch = true;
if (Array.isArray(dateFormat)) {
dateFormat.forEach((df) => {
let tryParseDate = parse(value, df, new Date(), {
locale: localeObject,
});
if (strictParsing) {
strictParsingValueMatch =
isValid(tryParseDate, minDate) &&
value === formatDate(tryParseDate, df, locale);
}
if (isValid(tryParseDate, minDate) && strictParsingValueMatch) {
parsedDate = tryParseDate;
}
});
return parsedDate;
}
parsedDate = parse(value, dateFormat, new Date(), { locale: localeObject });
if (strictParsing) {
strictParsingValueMatch =
isValid(parsedDate) &&
value === formatDate(parsedDate, dateFormat, locale);
} else if (!isValid(parsedDate)) {
dateFormat = dateFormat
.match(longFormattingTokensRegExp)
.map(function (substring) {
var firstCharacter = substring[0];
if (firstCharacter === "p" || firstCharacter === "P") {
var longFormatter = longFormatters[firstCharacter];
return localeObject
? longFormatter(substring, localeObject.formatLong)
: firstCharacter;
}
return substring;
})
.join("");
if (value.length > 0) {
parsedDate = parse(value, dateFormat.slice(0, value.length), new Date());
}
if (!isValid(parsedDate)) {
parsedDate = new Date(value);
}
}
return isValid(parsedDate) && strictParsingValueMatch ? parsedDate : null;
}
// ** Date "Reflection" **
export { isDate };
export function isValid(date, minDate) {
minDate = minDate ? minDate : new Date("1/1/1000");
return isValidDate(date) && !isBefore(date, minDate);
}
// ** Date Formatting **
export function formatDate(date, formatStr, locale) {
if (locale === "en") {
return format(date, formatStr, { awareOfUnicodeTokens: true });
}
let localeObj = getLocaleObject(locale);
if (locale && !localeObj) {
console.warn(
`A locale object was not found for the provided string ["${locale}"].`
);
}
if (
!localeObj &&
!!getDefaultLocale() &&
!!getLocaleObject(getDefaultLocale())
) {
localeObj = getLocaleObject(getDefaultLocale());
}
return format(date, formatStr, {
locale: localeObj ? localeObj : null,
awareOfUnicodeTokens: true,
});
}
export function safeDateFormat(date, { dateFormat, locale }) {
return (
(date &&
formatDate(
date,
Array.isArray(dateFormat) ? dateFormat[0] : dateFormat,
locale
)) ||
""
);
}
export function safeDateRangeFormat(startDate, endDate, props) {
if (!startDate) {
return "";
}
const formattedStartDate = safeDateFormat(startDate, props);
const formattedEndDate = endDate ? safeDateFormat(endDate, props) : "";
return `${formattedStartDate} - ${formattedEndDate}`;
}
// ** Date Setters **
export function setTime(date, { hour = 0, minute = 0, second = 0 }) {
return setHours(setMinutes(setSeconds(date, second), minute), hour);
}
export { setMinutes, setHours, setMonth, setQuarter, setYear };
// ** Date Getters **
// getDay Returns day of week, getDate returns day of month
export {
getSeconds,
getMinutes,
getHours,
getMonth,
getQuarter,
getYear,
getDay,
getDate,
getTime,
};
export function getWeek(date, locale) {
let localeObj =
(locale && getLocaleObject(locale)) ||
(getDefaultLocale() && getLocaleObject(getDefaultLocale()));
return getISOWeek(date, localeObj ? { locale: localeObj } : null);
}
export function getDayOfWeekCode(day, locale) {
return formatDate(day, "ddd", locale);
}
// *** Start of ***
export function getStartOfDay(date) {
return startOfDay(date);
}
export function getStartOfWeek(date, locale, calendarStartDay) {
let localeObj = locale
? getLocaleObject(locale)
: getLocaleObject(getDefaultLocale());
return startOfWeek(date, {
locale: localeObj,
weekStartsOn: calendarStartDay,
});
}
export function getStartOfMonth(date) {
return startOfMonth(date);
}
export function getStartOfYear(date) {
return startOfYear(date);
}
export function getStartOfQuarter(date) {
return startOfQuarter(date);
}
export function getStartOfToday() {
return startOfDay(newDate());
}
// *** End of ***
export function getEndOfWeek(date) {
return endOfWeek(date);
}
export function getEndOfMonth(date) {
return endOfMonth(date);
}
// ** Date Math **
// *** Addition ***
export { addMinutes, addDays, addWeeks, addMonths, addYears };
// *** Subtraction ***
export {
addHours,
subMinutes,
subHours,
subDays,
subWeeks,
subMonths,
subYears,
};
// ** Date Comparison **
export { isBefore, isAfter };
export function isSameYear(date1, date2) {
if (date1 && date2) {
return dfIsSameYear(date1, date2);
} else {
return !date1 && !date2;
}
}
export function isSameMonth(date1, date2) {
if (date1 && date2) {
return dfIsSameMonth(date1, date2);
} else {
return !date1 && !date2;
}
}
export function isSameQuarter(date1, date2) {
if (date1 && date2) {
return dfIsSameQuarter(date1, date2);
} else {
return !date1 && !date2;
}
}
export function isSameDay(date1, date2) {
if (date1 && date2) {
return dfIsSameDay(date1, date2);
} else {
return !date1 && !date2;
}
}
export function isEqual(date1, date2) {
if (date1 && date2) {
return dfIsEqual(date1, date2);
} else {
return !date1 && !date2;
}
}
export function isDayInRange(day, startDate, endDate) {
let valid;
const start = startOfDay(startDate);
const end = endOfDay(endDate);
try {
valid = isWithinInterval(day, { start, end });
} catch (err) {
valid = false;
}
return valid;
}
// *** Diffing ***
export function getDaysDiff(date1, date2) {
return differenceInCalendarDays(date1, date2);
}
// ** Date Localization **
export function registerLocale(localeName, localeData) {
const scope = typeof window !== "undefined" ? window : globalThis;
if (!scope.__localeData__) {
scope.__localeData__ = {};
}
scope.__localeData__[localeName] = localeData;
}
export function setDefaultLocale(localeName) {
const scope = typeof window !== "undefined" ? window : globalThis;
scope.__localeId__ = localeName;
}
export function getDefaultLocale() {
const scope = typeof window !== "undefined" ? window : globalThis;
return scope.__localeId__;
}
export function getLocaleObject(localeSpec) {
if (typeof localeSpec === "string") {
// Treat it as a locale name registered by registerLocale
const scope = typeof window !== "undefined" ? window : globalThis;
return scope.__localeData__ ? scope.__localeData__[localeSpec] : null;
} else {
// Treat it as a raw date-fns locale object
return localeSpec;
}
}
export function getFormattedWeekdayInLocale(date, formatFunc, locale) {
return typeof formatFunc === "function"
? formatFunc(date, locale)
: formatDate(date, "EEEE", locale);
}
export function getWeekdayMinInLocale(date, locale) {
return formatDate(date, "EEEEEE", locale);
}
export function getWeekdayShortInLocale(date, locale) {
return formatDate(date, "EEE", locale);
}
export function getMonthInLocale(month, locale) {
return formatDate(setMonth(newDate(), month), "LLLL", locale);
}
export function getMonthShortInLocale(month, locale) {
return formatDate(setMonth(newDate(), month), "LLL", locale);
}
export function getQuarterShortInLocale(quarter, locale) {
return formatDate(setQuarter(newDate(), quarter), "QQQ", locale);
}
// ** Utils for some components **
export function isDayDisabled(
day,
{
minDate,
maxDate,
excludeDates,
excludeDateIntervals,
includeDates,
includeDateIntervals,
filterDate,
} = {}
) {
return (
isOutOfBounds(day, { minDate, maxDate }) ||
(excludeDates &&
excludeDates.some((excludeDate) => isSameDay(day, excludeDate))) ||
(excludeDateIntervals &&
excludeDateIntervals.some(({ start, end }) =>
isWithinInterval(day, { start, end })
)) ||
(includeDates &&
!includeDates.some((includeDate) => isSameDay(day, includeDate))) ||
(includeDateIntervals &&
!includeDateIntervals.some(({ start, end }) =>
isWithinInterval(day, { start, end })
)) ||
(filterDate && !filterDate(newDate(day))) ||
false
);
}
export function isDayExcluded(
day,
{ excludeDates, excludeDateIntervals } = {}
) {
if (excludeDateIntervals && excludeDateIntervals.length > 0) {
return excludeDateIntervals.some(({ start, end }) =>
isWithinInterval(day, { start, end })
);
}
return (
(excludeDates &&
excludeDates.some((excludeDate) => isSameDay(day, excludeDate))) ||
false
);
}
export function isMonthDisabled(
month,
{ minDate, maxDate, excludeDates, includeDates, filterDate } = {}
) {
return (
isOutOfBounds(month, {
minDate: startOfMonth(minDate),
maxDate: endOfMonth(maxDate),
}) ||
(excludeDates &&
excludeDates.some((excludeDate) => isSameMonth(month, excludeDate))) ||
(includeDates &&
!includeDates.some((includeDate) => isSameMonth(month, includeDate))) ||
(filterDate && !filterDate(newDate(month))) ||
false
);
}
export function isMonthinRange(startDate, endDate, m, day) {
const startDateYear = getYear(startDate);
const startDateMonth = getMonth(startDate);
const endDateYear = getYear(endDate);
const endDateMonth = getMonth(endDate);
const dayYear = getYear(day);
if (startDateYear === endDateYear && startDateYear === dayYear) {
return startDateMonth <= m && m <= endDateMonth;
} else if (startDateYear < endDateYear) {
return (
(dayYear === startDateYear && startDateMonth <= m) ||
(dayYear === endDateYear && endDateMonth >= m) ||
(dayYear < endDateYear && dayYear > startDateYear)
);
}
}
export function isQuarterDisabled(
quarter,
{ minDate, maxDate, excludeDates, includeDates, filterDate } = {}
) {
return (
isOutOfBounds(quarter, { minDate, maxDate }) ||
(excludeDates &&
excludeDates.some((excludeDate) =>
isSameQuarter(quarter, excludeDate)
)) ||
(includeDates &&
!includeDates.some((includeDate) =>
isSameQuarter(quarter, includeDate)
)) ||
(filterDate && !filterDate(newDate(quarter))) ||
false
);
}
export function isYearDisabled(
year,
{ minDate, maxDate, excludeDates, includeDates, filterDate } = {}
) {
const date = new Date(year, 0, 1);
return (
isOutOfBounds(date, {
minDate: startOfYear(minDate),
maxDate: endOfYear(maxDate),
}) ||
(excludeDates &&
excludeDates.some((excludeDate) => isSameYear(date, excludeDate))) ||
(includeDates &&
!includeDates.some((includeDate) => isSameYear(date, includeDate))) ||
(filterDate && !filterDate(newDate(date))) ||
false
);
}
export function isQuarterInRange(startDate, endDate, q, day) {
const startDateYear = getYear(startDate);
const startDateQuarter = getQuarter(startDate);
const endDateYear = getYear(endDate);
const endDateQuarter = getQuarter(endDate);
const dayYear = getYear(day);
if (startDateYear === endDateYear && startDateYear === dayYear) {
return startDateQuarter <= q && q <= endDateQuarter;
} else if (startDateYear < endDateYear) {
return (
(dayYear === startDateYear && startDateQuarter <= q) ||
(dayYear === endDateYear && endDateQuarter >= q) ||
(dayYear < endDateYear && dayYear > startDateYear)
);
}
}
export function isOutOfBounds(day, { minDate, maxDate } = {}) {
return (
(minDate && differenceInCalendarDays(day, minDate) < 0) ||
(maxDate && differenceInCalendarDays(day, maxDate) > 0)
);
}
export function isTimeInList(time, times) {
return times.some(
(listTime) =>
getHours(listTime) === getHours(time) &&
getMinutes(listTime) === getMinutes(time)
);
}
export function isTimeDisabled(
time,
{ excludeTimes, includeTimes, filterTime } = {}
) {
return (
(excludeTimes && isTimeInList(time, excludeTimes)) ||
(includeTimes && !isTimeInList(time, includeTimes)) ||
(filterTime && !filterTime(time)) ||
false
);
}
export function isTimeInDisabledRange(time, { minTime, maxTime }) {
if (!minTime || !maxTime) {
throw new Error("Both minTime and maxTime props required");
}
const base = newDate();
const baseTime = setHours(setMinutes(base, getMinutes(time)), getHours(time));
const min = setHours(
setMinutes(base, getMinutes(minTime)),
getHours(minTime)
);
const max = setHours(
setMinutes(base, getMinutes(maxTime)),
getHours(maxTime)
);
let valid;
try {
valid = !isWithinInterval(baseTime, { start: min, end: max });
} catch (err) {
valid = false;
}
return valid;
}
export function monthDisabledBefore(day, { minDate, includeDates } = {}) {
const previousMonth = subMonths(day, 1);
return (
(minDate && differenceInCalendarMonths(minDate, previousMonth) > 0) ||
(includeDates &&
includeDates.every(
(includeDate) =>
differenceInCalendarMonths(includeDate, previousMonth) > 0
)) ||
false
);
}
export function monthDisabledAfter(day, { maxDate, includeDates } = {}) {
const nextMonth = addMonths(day, 1);
return (
(maxDate && differenceInCalendarMonths(nextMonth, maxDate) > 0) ||
(includeDates &&
includeDates.every(
(includeDate) => differenceInCalendarMonths(nextMonth, includeDate) > 0
)) ||
false
);
}
export function yearDisabledBefore(day, { minDate, includeDates } = {}) {
const previousYear = subYears(day, 1);
return (
(minDate && differenceInCalendarYears(minDate, previousYear) > 0) ||
(includeDates &&
includeDates.every(
(includeDate) =>
differenceInCalendarYears(includeDate, previousYear) > 0
)) ||
false
);
}
export function yearsDisabledBefore(
day,
{ minDate, yearItemNumber = DEFAULT_YEAR_ITEM_NUMBER } = {}
) {
const previousYear = getStartOfYear(subYears(day, yearItemNumber));
const { endPeriod } = getYearsPeriod(previousYear, yearItemNumber);
const minDateYear = minDate && getYear(minDate);
return (minDateYear && minDateYear > endPeriod) || false;
}
export function yearDisabledAfter(day, { maxDate, includeDates } = {}) {
const nextYear = addYears(day, 1);
return (
(maxDate && differenceInCalendarYears(nextYear, maxDate) > 0) ||
(includeDates &&
includeDates.every(
(includeDate) => differenceInCalendarYears(nextYear, includeDate) > 0
)) ||
false
);
}
export function yearsDisabledAfter(
day,
{ maxDate, yearItemNumber = DEFAULT_YEAR_ITEM_NUMBER } = {}
) {
const nextYear = addYears(day, yearItemNumber);
const { startPeriod } = getYearsPeriod(nextYear, yearItemNumber);
const maxDateYear = maxDate && getYear(maxDate);
return (maxDateYear && maxDateYear < startPeriod) || false;
}
export function getEffectiveMinDate({ minDate, includeDates }) {
if (includeDates && minDate) {
let minDates = includeDates.filter(
(includeDate) => differenceInCalendarDays(includeDate, minDate) >= 0
);
return min(minDates);
} else if (includeDates) {
return min(includeDates);
} else {
return minDate;
}
}
export function getEffectiveMaxDate({ maxDate, includeDates }) {
if (includeDates && maxDate) {
let maxDates = includeDates.filter(
(includeDate) => differenceInCalendarDays(includeDate, maxDate) <= 0
);
return max(maxDates);
} else if (includeDates) {
return max(includeDates);
} else {
return maxDate;
}
}
export function getHightLightDaysMap(
highlightDates = [],
defaultClassName = "react-datepicker__day--highlighted"
) {
const dateClasses = new Map();
for (let i = 0, len = highlightDates.length; i < len; i++) {
const obj = highlightDates[i];
if (isDate(obj)) {
const key = formatDate(obj, "MM.dd.yyyy");
const classNamesArr = dateClasses.get(key) || [];
if (!classNamesArr.includes(defaultClassName)) {
classNamesArr.push(defaultClassName);
dateClasses.set(key, classNamesArr);
}
} else if (typeof obj === "object") {
const keys = Object.keys(obj);
const className = keys[0];
const arrOfDates = obj[keys[0]];
if (typeof className === "string" && arrOfDates.constructor === Array) {
for (let k = 0, len = arrOfDates.length; k < len; k++) {
const key = formatDate(arrOfDates[k], "MM.dd.yyyy");
const classNamesArr = dateClasses.get(key) || [];
if (!classNamesArr.includes(className)) {
classNamesArr.push(className);
dateClasses.set(key, classNamesArr);
}
}
}
}
}
return dateClasses;
}
export function timesToInjectAfter(
startOfDay,
currentTime,
currentMultiplier,
intervals,
injectedTimes
) {
const l = injectedTimes.length;
const times = [];
for (let i = 0; i < l; i++) {
const injectedTime = addMinutes(
addHours(startOfDay, getHours(injectedTimes[i])),
getMinutes(injectedTimes[i])
);
const nextTime = addMinutes(
startOfDay,
(currentMultiplier + 1) * intervals
);
if (
isAfter(injectedTime, currentTime) &&
isBefore(injectedTime, nextTime)
) {
times.push(injectedTimes[i]);
}
}
return times;
}
export function addZero(i) {
return i < 10 ? `0${i}` : `${i}`;
}
export function getYearsPeriod(
date,
yearItemNumber = DEFAULT_YEAR_ITEM_NUMBER
) {
const endPeriod = Math.ceil(getYear(date) / yearItemNumber) * yearItemNumber;
const startPeriod = endPeriod - (yearItemNumber - 1);
return { startPeriod, endPeriod };
}