Files
klz-cables.com/.pnpm-store/v10/files/0e/92d01daa5a0dd66de78f7175b345798ee90881fbc330ddf02c85c50be62086e85d3899a8ed27426e2b2cf0efeef78f9130af77cf7d58286a981d10ed700b6b
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

62 lines
1.7 KiB
Plaintext

import { normalizeDates } from "./_lib/normalizeDates.js";
import { compareAsc } from "./compareAsc.js";
import { differenceInCalendarMonths } from "./differenceInCalendarMonths.js";
import { isLastDayOfMonth } from "./isLastDayOfMonth.js";
/**
* The {@link differenceInMonths} function options.
*/
/**
* @name differenceInMonths
* @category Month Helpers
* @summary Get the number of full months between the given dates.
*
* @param laterDate - The later date
* @param earlierDate - The earlier date
* @param options - An object with options
*
* @returns The number of full months
*
* @example
* // How many full months are between 31 January 2014 and 1 September 2014?
* const result = differenceInMonths(new Date(2014, 8, 1), new Date(2014, 0, 31))
* //=> 7
*/
export function differenceInMonths(laterDate, earlierDate, options) {
const [laterDate_, workingLaterDate, earlierDate_] = normalizeDates(
options?.in,
laterDate,
laterDate,
earlierDate,
);
const sign = compareAsc(workingLaterDate, earlierDate_);
const difference = Math.abs(
differenceInCalendarMonths(workingLaterDate, earlierDate_),
);
if (difference < 1) return 0;
if (workingLaterDate.getMonth() === 1 && workingLaterDate.getDate() > 27)
workingLaterDate.setDate(30);
workingLaterDate.setMonth(workingLaterDate.getMonth() - sign * difference);
let isLastMonthNotFull = compareAsc(workingLaterDate, earlierDate_) === -sign;
if (
isLastDayOfMonth(laterDate_) &&
difference === 1 &&
compareAsc(laterDate_, earlierDate_) === 1
) {
isLastMonthNotFull = false;
}
const result = sign * (difference - +isLastMonthNotFull);
return result === 0 ? 0 : result;
}
// Fallback for modularized imports:
export default differenceInMonths;