UNPKG

1.61 kBJavaScriptView Raw
1import { startOfSecond } from "./startOfSecond.mjs";
2
3/**
4 * @name isSameSecond
5 * @category Second Helpers
6 * @summary Are the given dates in the same second (and hour and day)?
7 *
8 * @description
9 * Are the given dates in the same second (and hour and 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 second (and hour and day)
17 *
18 * @example
19 * // Are 4 September 2014 06:30:15.000 and 4 September 2014 06:30.15.500 in the same second?
20 * const result = isSameSecond(
21 * new Date(2014, 8, 4, 6, 30, 15),
22 * new Date(2014, 8, 4, 6, 30, 15, 500)
23 * )
24 * //=> true
25 *
26 * @example
27 * // Are 4 September 2014 06:00:15.000 and 4 September 2014 06:01.15.000 in the same second?
28 * const result = isSameSecond(
29 * new Date(2014, 8, 4, 6, 0, 15),
30 * new Date(2014, 8, 4, 6, 1, 15)
31 * )
32 * //=> false
33 *
34 * @example
35 * // Are 4 September 2014 06:00:15.000 and 5 September 2014 06:00.15.000 in the same second?
36 * const result = isSameSecond(
37 * new Date(2014, 8, 4, 6, 0, 15),
38 * new Date(2014, 8, 5, 6, 0, 15)
39 * )
40 * //=> false
41 */
42export function isSameSecond(dateLeft, dateRight) {
43 const dateLeftStartOfSecond = startOfSecond(dateLeft);
44 const dateRightStartOfSecond = startOfSecond(dateRight);
45
46 return +dateLeftStartOfSecond === +dateRightStartOfSecond;
47}
48
49// Fallback for modularized imports:
50export default isSameSecond;