addMonths.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. exports.addMonths = addMonths;
  3. var _index = require("./toDate.js");
  4. var _index2 = require("./constructFrom.js");
  5. /**
  6. * @name addMonths
  7. * @category Month Helpers
  8. * @summary Add the specified number of months to the given date.
  9. *
  10. * @description
  11. * Add the specified number of months to the given date.
  12. *
  13. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  14. *
  15. * @param date - The date to be changed
  16. * @param amount - The amount of months to be added.
  17. *
  18. * @returns The new date with the months added
  19. *
  20. * @example
  21. * // Add 5 months to 1 September 2014:
  22. * const result = addMonths(new Date(2014, 8, 1), 5)
  23. * //=> Sun Feb 01 2015 00:00:00
  24. *
  25. * // Add one month to 30 January 2023:
  26. * const result = addMonths(new Date(2023, 0, 30), 1)
  27. * //=> Tue Feb 28 2023 00:00:00
  28. */
  29. function addMonths(date, amount) {
  30. const _date = (0, _index.toDate)(date);
  31. if (isNaN(amount)) return (0, _index2.constructFrom)(date, NaN);
  32. if (!amount) {
  33. // If 0 months, no-op to avoid changing times in the hour before end of DST
  34. return _date;
  35. }
  36. const dayOfMonth = _date.getDate();
  37. // The JS Date object supports date math by accepting out-of-bounds values for
  38. // month, day, etc. For example, new Date(2020, 0, 0) returns 31 Dec 2019 and
  39. // new Date(2020, 13, 1) returns 1 Feb 2021. This is *almost* the behavior we
  40. // want except that dates will wrap around the end of a month, meaning that
  41. // new Date(2020, 13, 31) will return 3 Mar 2021 not 28 Feb 2021 as desired. So
  42. // we'll default to the end of the desired month by adding 1 to the desired
  43. // month and using a date of 0 to back up one day to the end of the desired
  44. // month.
  45. const endOfDesiredMonth = (0, _index2.constructFrom)(date, _date.getTime());
  46. endOfDesiredMonth.setMonth(_date.getMonth() + amount + 1, 0);
  47. const daysInMonth = endOfDesiredMonth.getDate();
  48. if (dayOfMonth >= daysInMonth) {
  49. // If we're already at the end of the month, then this is the correct date
  50. // and we're done.
  51. return endOfDesiredMonth;
  52. } else {
  53. // Otherwise, we now know that setting the original day-of-month value won't
  54. // cause an overflow, so set the desired day-of-month. Note that we can't
  55. // just set the date of `endOfDesiredMonth` because that object may have had
  56. // its time changed in the unusual case where where a DST transition was on
  57. // the last day of the month and its local time was in the hour skipped or
  58. // repeated next to a DST transition. So we use `date` instead which is
  59. // guaranteed to still have the original time.
  60. _date.setFullYear(
  61. endOfDesiredMonth.getFullYear(),
  62. endOfDesiredMonth.getMonth(),
  63. dayOfMonth,
  64. );
  65. return _date;
  66. }
  67. }