index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import toDate from "../toDate/index.js";
  2. import differenceInCalendarMonths from "../differenceInCalendarMonths/index.js";
  3. import compareAsc from "../compareAsc/index.js";
  4. import requiredArgs from "../_lib/requiredArgs/index.js";
  5. import isLastDayOfMonth from "../isLastDayOfMonth/index.js";
  6. /**
  7. * @name differenceInMonths
  8. * @category Month Helpers
  9. * @summary Get the number of full months between the given dates.
  10. *
  11. * @description
  12. * Get the number of full months between the given dates using trunc as a default rounding method.
  13. *
  14. * @param {Date|Number} dateLeft - the later date
  15. * @param {Date|Number} dateRight - the earlier date
  16. * @returns {Number} the number of full months
  17. * @throws {TypeError} 2 arguments required
  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 default function differenceInMonths(dirtyDateLeft, dirtyDateRight) {
  25. requiredArgs(2, arguments);
  26. var dateLeft = toDate(dirtyDateLeft);
  27. var dateRight = toDate(dirtyDateRight);
  28. var sign = compareAsc(dateLeft, dateRight);
  29. var difference = Math.abs(differenceInCalendarMonths(dateLeft, dateRight));
  30. var result;
  31. // Check for the difference of less than month
  32. if (difference < 1) {
  33. result = 0;
  34. } else {
  35. if (dateLeft.getMonth() === 1 && dateLeft.getDate() > 27) {
  36. // This will check if the date is end of Feb and assign a higher end of month date
  37. // to compare it with Jan
  38. dateLeft.setDate(30);
  39. }
  40. dateLeft.setMonth(dateLeft.getMonth() - sign * difference);
  41. // Math.abs(diff in full months - diff in calendar months) === 1 if last calendar month is not full
  42. // If so, result must be decreased by 1 in absolute value
  43. var isLastMonthNotFull = compareAsc(dateLeft, dateRight) === -sign;
  44. // Check for cases of one full calendar month
  45. if (isLastDayOfMonth(toDate(dirtyDateLeft)) && difference === 1 && compareAsc(dirtyDateLeft, dateRight) === 1) {
  46. isLastMonthNotFull = false;
  47. }
  48. result = sign * (difference - Number(isLastMonthNotFull));
  49. }
  50. // Prevent negative zero
  51. return result === 0 ? 0 : result;
  52. }