index.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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("../_lib/requiredArgs/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * @name addMonths
  12. * @category Month Helpers
  13. * @summary Add the specified number of months to the given date.
  14. *
  15. * @description
  16. * Add the specified number of months to the given date.
  17. *
  18. * ### v2.0.0 breaking changes:
  19. *
  20. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  21. *
  22. * @param {Date|Number} date - the date to be changed
  23. * @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`.
  24. * @returns {Date} the new date with the months added
  25. * @throws {TypeError} 2 arguments required
  26. *
  27. * @example
  28. * // Add 5 months to 1 September 2014:
  29. * const result = addMonths(new Date(2014, 8, 1), 5)
  30. * //=> Sun Feb 01 2015 00:00:00
  31. */
  32. function addMonths(dirtyDate, dirtyAmount) {
  33. (0, _index3.default)(2, arguments);
  34. var date = (0, _index2.default)(dirtyDate);
  35. var amount = (0, _index.default)(dirtyAmount);
  36. if (isNaN(amount)) {
  37. return new Date(NaN);
  38. }
  39. if (!amount) {
  40. // If 0 months, no-op to avoid changing times in the hour before end of DST
  41. return date;
  42. }
  43. var dayOfMonth = date.getDate(); // The JS Date object supports date math by accepting out-of-bounds values for
  44. // month, day, etc. For example, new Date(2020, 0, 0) returns 31 Dec 2019 and
  45. // new Date(2020, 13, 1) returns 1 Feb 2021. This is *almost* the behavior we
  46. // want except that dates will wrap around the end of a month, meaning that
  47. // new Date(2020, 13, 31) will return 3 Mar 2021 not 28 Feb 2021 as desired. So
  48. // we'll default to the end of the desired month by adding 1 to the desired
  49. // month and using a date of 0 to back up one day to the end of the desired
  50. // month.
  51. var endOfDesiredMonth = new Date(date.getTime());
  52. endOfDesiredMonth.setMonth(date.getMonth() + amount + 1, 0);
  53. var daysInMonth = endOfDesiredMonth.getDate();
  54. if (dayOfMonth >= daysInMonth) {
  55. // If we're already at the end of the month, then this is the correct date
  56. // and we're done.
  57. return endOfDesiredMonth;
  58. } else {
  59. // Otherwise, we now know that setting the original day-of-month value won't
  60. // cause an overflow, so set the desired day-of-month. Note that we can't
  61. // just set the date of `endOfDesiredMonth` because that object may have had
  62. // its time changed in the unusual case where where a DST transition was on
  63. // the last day of the month and its local time was in the hour skipped or
  64. // repeated next to a DST transition. So we use `date` instead which is
  65. // guaranteed to still have the original time.
  66. date.setFullYear(endOfDesiredMonth.getFullYear(), endOfDesiredMonth.getMonth(), dayOfMonth);
  67. return date;
  68. }
  69. }
  70. module.exports = exports.default;