/* eslint-disable eslint-comments/no-unlimited-disable */ /* eslint-disable */ // @ts-nocheck import { normalizeDates } from '../_lib/normalizeDates/index.ts'; import { compareAsc } from '../compareAsc/index.ts'; import { differenceInCalendarMonths } from '../differenceInCalendarMonths/index.ts'; import { isLastDayOfMonth } from '../isLastDayOfMonth/index.ts'; import type { ContextOptions, DateArg } from '../types.ts'; /** * The {@link differenceInMonths} function options. */ export interface DifferenceInMonthsOptions extends ContextOptions {} /** * @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: DateArg & {}, earlierDate: DateArg & {}, options?: DifferenceInMonthsOptions | undefined, ): number { 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; } /* eslint-enable */