index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = differenceInCalendarMonths;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name differenceInCalendarMonths
  11. * @category Month Helpers
  12. * @summary Get the number of calendar months between the given dates.
  13. *
  14. * @description
  15. * Get the number of calendar months between the given dates.
  16. *
  17. * ### v2.0.0 breaking changes:
  18. *
  19. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  20. *
  21. * @param {Date|Number} dateLeft - the later date
  22. * @param {Date|Number} dateRight - the earlier date
  23. * @returns {Number} the number of calendar months
  24. * @throws {TypeError} 2 arguments required
  25. *
  26. * @example
  27. * // How many calendar months are between 31 January 2014 and 1 September 2014?
  28. * var result = differenceInCalendarMonths(
  29. * new Date(2014, 8, 1),
  30. * new Date(2014, 0, 31)
  31. * )
  32. * //=> 8
  33. */
  34. function differenceInCalendarMonths(dirtyDateLeft, dirtyDateRight) {
  35. (0, _index2.default)(2, arguments);
  36. var dateLeft = (0, _index.default)(dirtyDateLeft);
  37. var dateRight = (0, _index.default)(dirtyDateRight);
  38. var yearDiff = dateLeft.getFullYear() - dateRight.getFullYear();
  39. var monthDiff = dateLeft.getMonth() - dateRight.getMonth();
  40. return yearDiff * 12 + monthDiff;
  41. }
  42. module.exports = exports.default;