1 | import { startOfHour } from "./startOfHour.mjs";
|
2 |
|
3 | /**
|
4 | * @name isSameHour
|
5 | * @category Hour Helpers
|
6 | * @summary Are the given dates in the same hour (and same day)?
|
7 | *
|
8 | * @description
|
9 | * Are the given dates in the same hour (and same day)?
|
10 | *
|
11 | * @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).
|
12 | *
|
13 | * @param dateLeft - The first date to check
|
14 | * @param dateRight - The second date to check
|
15 | *
|
16 | * @returns The dates are in the same hour (and same day)
|
17 | *
|
18 | * @example
|
19 | * // Are 4 September 2014 06:00:00 and 4 September 06:30:00 in the same hour?
|
20 | * const result = isSameHour(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 4, 6, 30))
|
21 | * //=> true
|
22 | *
|
23 | * @example
|
24 | * // Are 4 September 2014 06:00:00 and 5 September 06:00:00 in the same hour?
|
25 | * const result = isSameHour(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 5, 6, 0))
|
26 | * //=> false
|
27 | */
|
28 | export function isSameHour(dateLeft, dateRight) {
|
29 | const dateLeftStartOfHour = startOfHour(dateLeft);
|
30 | const dateRightStartOfHour = startOfHour(dateRight);
|
31 |
|
32 | return +dateLeftStartOfHour === +dateRightStartOfHour;
|
33 | }
|
34 |
|
35 | // Fallback for modularized imports:
|
36 | export default isSameHour;
|