index.js 3.0 KB

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