UNPKG

1.29 kBJavaScriptView Raw
1import { getRoundingMethod } from "./_lib/getRoundingMethod.mjs";
2import { millisecondsInHour } from "./constants.mjs";
3import { differenceInMilliseconds } from "./differenceInMilliseconds.mjs";
4
5/**
6 * The {@link differenceInHours} function options.
7 */
8
9/**
10 * @name differenceInHours
11 * @category Hour Helpers
12 * @summary Get the number of hours between the given dates.
13 *
14 * @description
15 * Get the number of hours between the given dates.
16 *
17 * @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).
18 *
19 * @param dateLeft - The later date
20 * @param dateRight - The earlier date
21 * @param options - An object with options.
22 *
23 * @returns The number of hours
24 *
25 * @example
26 * // How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00?
27 * const result = differenceInHours(
28 * new Date(2014, 6, 2, 19, 0),
29 * new Date(2014, 6, 2, 6, 50)
30 * )
31 * //=> 12
32 */
33export function differenceInHours(dateLeft, dateRight, options) {
34 const diff =
35 differenceInMilliseconds(dateLeft, dateRight) / millisecondsInHour;
36 return getRoundingMethod(options?.roundingMethod)(diff);
37}
38
39// Fallback for modularized imports:
40export default differenceInHours;