addMonths.cjs 3.1 KB

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