differenceInMonths.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { normalizeDates } from "./_lib/normalizeDates.js";
  2. import { compareAsc } from "./compareAsc.js";
  3. import { differenceInCalendarMonths } from "./differenceInCalendarMonths.js";
  4. import { isLastDayOfMonth } from "./isLastDayOfMonth.js";
  5. /**
  6. * The {@link differenceInMonths} function options.
  7. */
  8. /**
  9. * @name differenceInMonths
  10. * @category Month Helpers
  11. * @summary Get the number of full months between the given dates.
  12. *
  13. * @param laterDate - The later date
  14. * @param earlierDate - The earlier date
  15. * @param options - An object with options
  16. *
  17. * @returns The number of full months
  18. *
  19. * @example
  20. * // How many full months are between 31 January 2014 and 1 September 2014?
  21. * const result = differenceInMonths(new Date(2014, 8, 1), new Date(2014, 0, 31))
  22. * //=> 7
  23. */
  24. export function differenceInMonths(laterDate, earlierDate, options) {
  25. const [laterDate_, workingLaterDate, earlierDate_] = normalizeDates(
  26. options?.in,
  27. laterDate,
  28. laterDate,
  29. earlierDate,
  30. );
  31. const sign = compareAsc(workingLaterDate, earlierDate_);
  32. const difference = Math.abs(
  33. differenceInCalendarMonths(workingLaterDate, earlierDate_),
  34. );
  35. if (difference < 1) return 0;
  36. if (workingLaterDate.getMonth() === 1 && workingLaterDate.getDate() > 27)
  37. workingLaterDate.setDate(30);
  38. workingLaterDate.setMonth(workingLaterDate.getMonth() - sign * difference);
  39. let isLastMonthNotFull = compareAsc(workingLaterDate, earlierDate_) === -sign;
  40. if (
  41. isLastDayOfMonth(laterDate_) &&
  42. difference === 1 &&
  43. compareAsc(laterDate_, earlierDate_) === 1
  44. ) {
  45. isLastMonthNotFull = false;
  46. }
  47. const result = sign * (difference - +isLastMonthNotFull);
  48. return result === 0 ? 0 : result;
  49. }
  50. // Fallback for modularized imports:
  51. export default differenceInMonths;