UNPKG

1.18 kBJavaScriptView Raw
1import { getRoundingMethod } from "./_lib/getRoundingMethod.js";
2import { normalizeDates } from "./_lib/normalizeDates.js";
3import { millisecondsInHour } from "./constants.js";
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 * @param laterDate - The later date
18 * @param earlierDate - The earlier date
19 * @param options - An object with options.
20 *
21 * @returns The number of hours
22 *
23 * @example
24 * // How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00?
25 * const result = differenceInHours(
26 * new Date(2014, 6, 2, 19, 0),
27 * new Date(2014, 6, 2, 6, 50)
28 * )
29 * //=> 12
30 */
31export function differenceInHours(laterDate, earlierDate, options) {
32 const [laterDate_, earlierDate_] = normalizeDates(
33 options?.in,
34 laterDate,
35 earlierDate,
36 );
37 const diff = (+laterDate_ - +earlierDate_) / millisecondsInHour;
38 return getRoundingMethod(options?.roundingMethod)(diff);
39}
40
41// Fallback for modularized imports:
42export default differenceInHours;