UNPKG

1.25 kBJavaScriptView Raw
1import { normalizeDates } from "./_lib/normalizeDates.js";
2import { getQuarter } from "./getQuarter.js";
3
4/**
5 * The {@link differenceInCalendarQuarters} function options.
6 */
7
8/**
9 * @name differenceInCalendarQuarters
10 * @category Quarter Helpers
11 * @summary Get the number of calendar quarters between the given dates.
12 *
13 * @description
14 * Get the number of calendar quarters between the given dates.
15 *
16 * @param laterDate - The later date
17 * @param earlierDate - The earlier date
18 * @param options - An object with options
19 *
20 * @returns The number of calendar quarters
21 *
22 * @example
23 * // How many calendar quarters are between 31 December 2013 and 2 July 2014?
24 * const result = differenceInCalendarQuarters(
25 * new Date(2014, 6, 2),
26 * new Date(2013, 11, 31)
27 * )
28 * //=> 3
29 */
30export function differenceInCalendarQuarters(laterDate, earlierDate, options) {
31 const [laterDate_, earlierDate_] = normalizeDates(
32 options?.in,
33 laterDate,
34 earlierDate,
35 );
36
37 const yearsDiff = laterDate_.getFullYear() - earlierDate_.getFullYear();
38 const quartersDiff = getQuarter(laterDate_) - getQuarter(earlierDate_);
39
40 return yearsDiff * 4 + quartersDiff;
41}
42
43// Fallback for modularized imports:
44export default differenceInCalendarQuarters;