index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = differenceInMonths;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../differenceInCalendarMonths/index.js"));
  8. var _index3 = _interopRequireDefault(require("../compareAsc/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. var _index5 = _interopRequireDefault(require("../isLastDayOfMonth/index.js"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * @name differenceInMonths
  14. * @category Month Helpers
  15. * @summary Get the number of full months between the given dates.
  16. *
  17. * @description
  18. * Get the number of full months between the given dates.
  19. *
  20. * ### v2.0.0 breaking changes:
  21. *
  22. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  23. *
  24. * @param {Date|Number} dateLeft - the later date
  25. * @param {Date|Number} dateRight - the earlier date
  26. * @returns {Number} the number of full months
  27. * @throws {TypeError} 2 arguments required
  28. *
  29. * @example
  30. * // How many full months are between 31 January 2014 and 1 September 2014?
  31. * var result = differenceInMonths(new Date(2014, 8, 1), new Date(2014, 0, 31))
  32. * //=> 7
  33. */
  34. function differenceInMonths(dirtyDateLeft, dirtyDateRight) {
  35. (0, _index4.default)(2, arguments);
  36. var dateLeft = (0, _index.default)(dirtyDateLeft);
  37. var dateRight = (0, _index.default)(dirtyDateRight);
  38. var sign = (0, _index3.default)(dateLeft, dateRight);
  39. var difference = Math.abs((0, _index2.default)(dateLeft, dateRight));
  40. var result; // Check for the difference of less than month
  41. if (difference < 1) {
  42. result = 0;
  43. } else {
  44. if (dateLeft.getMonth() === 1 && dateLeft.getDate() > 27) {
  45. // This will check if the date is end of Feb and assign a higher end of month date
  46. // to compare it with Jan
  47. dateLeft.setDate(30);
  48. }
  49. dateLeft.setMonth(dateLeft.getMonth() - sign * difference); // Math.abs(diff in full months - diff in calendar months) === 1 if last calendar month is not full
  50. // If so, result must be decreased by 1 in absolute value
  51. var isLastMonthNotFull = (0, _index3.default)(dateLeft, dateRight) === -sign; // Check for cases of one full calendar month
  52. if ((0, _index5.default)((0, _index.default)(dirtyDateLeft)) && difference === 1 && (0, _index3.default)(dirtyDateLeft, dateRight) === 1) {
  53. isLastMonthNotFull = false;
  54. }
  55. result = sign * (difference - Number(isLastMonthNotFull));
  56. } // Prevent negative zero
  57. return result === 0 ? 0 : result;
  58. }
  59. module.exports = exports.default;