addMonths.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { constructFrom } from "./constructFrom.js";
  2. import { toDate } from "./toDate.js";
  3. /**
  4. * The {@link addMonths} function options.
  5. */
  6. /**
  7. * @name addMonths
  8. * @category Month Helpers
  9. * @summary Add the specified number of months to the given date.
  10. *
  11. * @description
  12. * Add the specified number of months to the given date.
  13. *
  14. * @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).
  15. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  16. *
  17. * @param date - The date to be changed
  18. * @param amount - The amount of months to be added.
  19. * @param options - The options object
  20. *
  21. * @returns The new date with the months added
  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. * // Add one month to 30 January 2023:
  29. * const result = addMonths(new Date(2023, 0, 30), 1)
  30. * //=> Tue Feb 28 2023 00:00:00
  31. */
  32. export function addMonths(date, amount, options) {
  33. const _date = toDate(date, options?.in);
  34. if (isNaN(amount)) return constructFrom(options?.in || date, NaN);
  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. const 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. const endOfDesiredMonth = constructFrom(options?.in || date, _date.getTime());
  49. endOfDesiredMonth.setMonth(_date.getMonth() + amount + 1, 0);
  50. const 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(
  64. endOfDesiredMonth.getFullYear(),
  65. endOfDesiredMonth.getMonth(),
  66. dayOfMonth,
  67. );
  68. return _date;
  69. }
  70. }
  71. // Fallback for modularized imports:
  72. export default addMonths;