1 | ;
|
2 | exports.isWithinInterval = isWithinInterval;
|
3 | var _index = require("./toDate.js");
|
4 |
|
5 | /**
|
6 | * @name isWithinInterval
|
7 | * @category Interval Helpers
|
8 | * @summary Is the given date within the interval?
|
9 | *
|
10 | * @description
|
11 | * Is the given date within the interval? (Including start and end.)
|
12 | *
|
13 | * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
14 | *
|
15 | * @param date - The date to check
|
16 | * @param interval - The interval to check
|
17 | *
|
18 | * @returns The date is within the interval
|
19 | *
|
20 | * @example
|
21 | * // For the date within the interval:
|
22 | * isWithinInterval(new Date(2014, 0, 3), {
|
23 | * start: new Date(2014, 0, 1),
|
24 | * end: new Date(2014, 0, 7)
|
25 | * })
|
26 | * //=> true
|
27 | *
|
28 | * @example
|
29 | * // For the date outside of the interval:
|
30 | * isWithinInterval(new Date(2014, 0, 10), {
|
31 | * start: new Date(2014, 0, 1),
|
32 | * end: new Date(2014, 0, 7)
|
33 | * })
|
34 | * //=> false
|
35 | *
|
36 | * @example
|
37 | * // For date equal to interval start:
|
38 | * isWithinInterval(date, { start, end: date })
|
39 | * // => true
|
40 | *
|
41 | * @example
|
42 | * // For date equal to interval end:
|
43 | * isWithinInterval(date, { start: date, end })
|
44 | * // => true
|
45 | */
|
46 | function isWithinInterval(date, interval) {
|
47 | const time = +(0, _index.toDate)(date);
|
48 | const [startTime, endTime] = [
|
49 | +(0, _index.toDate)(interval.start),
|
50 | +(0, _index.toDate)(interval.end),
|
51 | ].sort((a, b) => a - b);
|
52 |
|
53 | return time >= startTime && time <= endTime;
|
54 | }
|