1 | import { normalizeDates } from "./_lib/normalizeDates.js";
|
2 | import { compareAsc } from "./compareAsc.js";
|
3 | import { differenceInCalendarISOWeekYears } from "./differenceInCalendarISOWeekYears.js";
|
4 | import { subISOWeekYears } from "./subISOWeekYears.js";
|
5 |
|
6 | /**
|
7 | * The {@link differenceInISOWeekYears} function options.
|
8 | */
|
9 |
|
10 | /**
|
11 | * @name differenceInISOWeekYears
|
12 | * @category ISO Week-Numbering Year Helpers
|
13 | * @summary Get the number of full ISO week-numbering years between the given dates.
|
14 | *
|
15 | * @description
|
16 | * Get the number of full ISO week-numbering years between the given dates.
|
17 | *
|
18 | * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
|
19 | *
|
20 | * @param laterDate - The later date
|
21 | * @param earlierDate - The earlier date
|
22 | * @param options - The options
|
23 | *
|
24 | * @returns The number of full ISO week-numbering years
|
25 | *
|
26 | * @example
|
27 | * // How many full ISO week-numbering years are between 1 January 2010 and 1 January 2012?
|
28 | * const result = differenceInISOWeekYears(
|
29 | * new Date(2012, 0, 1),
|
30 | * new Date(2010, 0, 1)
|
31 | * )
|
32 | * // => 1
|
33 | */
|
34 | export function differenceInISOWeekYears(laterDate, earlierDate, options) {
|
35 | const [laterDate_, earlierDate_] = normalizeDates(
|
36 | options?.in,
|
37 | laterDate,
|
38 | earlierDate,
|
39 | );
|
40 |
|
41 | const sign = compareAsc(laterDate_, earlierDate_);
|
42 | const diff = Math.abs(
|
43 | differenceInCalendarISOWeekYears(laterDate_, earlierDate_, options),
|
44 | );
|
45 |
|
46 | const adjustedDate = subISOWeekYears(laterDate_, sign * diff, options);
|
47 |
|
48 | const isLastISOWeekYearNotFull = Number(
|
49 | compareAsc(adjustedDate, earlierDate_) === -sign,
|
50 | );
|
51 | const result = sign * (diff - isLastISOWeekYearNotFull);
|
52 |
|
53 | // Prevent negative zero
|
54 | return result === 0 ? 0 : result;
|
55 | }
|
56 |
|
57 | // Fallback for modularized imports:
|
58 | export default differenceInISOWeekYears;
|