addMonths.mjs 2.8 KB

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