Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

55 строки
2.0 KiB

  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = addMonths;
  6. var _index = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../getDaysInMonth/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * @name addMonths
  13. * @category Month Helpers
  14. * @summary Add the specified number of months to the given date.
  15. *
  16. * @description
  17. * Add the specified number of months to the given date.
  18. *
  19. * ### v2.0.0 breaking changes:
  20. *
  21. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  22. *
  23. * @param {Date|Number} date - the date to be changed
  24. * @param {Number} amount - the amount of months to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
  25. * @returns {Date} the new date with the months added
  26. * @throws {TypeError} 2 arguments required
  27. *
  28. * @example
  29. * // Add 5 months to 1 September 2014:
  30. * var result = addMonths(new Date(2014, 8, 1), 5)
  31. * //=> Sun Feb 01 2015 00:00:00
  32. */
  33. function addMonths(dirtyDate, dirtyAmount) {
  34. (0, _index4.default)(2, arguments);
  35. var date = (0, _index2.default)(dirtyDate);
  36. var amount = (0, _index.default)(dirtyAmount);
  37. var desiredMonth = date.getMonth() + amount;
  38. var dateWithDesiredMonth = new Date(0);
  39. dateWithDesiredMonth.setFullYear(date.getFullYear(), desiredMonth, 1);
  40. dateWithDesiredMonth.setHours(0, 0, 0, 0);
  41. var daysInMonth = (0, _index3.default)(dateWithDesiredMonth); // Set the last day of the new month
  42. // if the original date was the last day of the longer month
  43. date.setMonth(desiredMonth, Math.min(daysInMonth, date.getDate()));
  44. return date;
  45. }
  46. module.exports = exports.default;