index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import toInteger from "../_lib/toInteger/index.js";
  2. import toDate from "../toDate/index.js";
  3. import requiredArgs from "../_lib/requiredArgs/index.js";
  4. /**
  5. * @name addMonths
  6. * @category Month Helpers
  7. * @summary Add the specified number of months to the given date.
  8. *
  9. * @description
  10. * Add the specified number of months to the given date.
  11. *
  12. * ### v2.0.0 breaking changes:
  13. *
  14. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  15. *
  16. * @param {Date|Number} date - the date to be changed
  17. * @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`.
  18. * @returns {Date} the new date with the months added
  19. * @throws {TypeError} 2 arguments required
  20. *
  21. * @example
  22. * // Add 5 months to 1 September 2014:
  23. * const result = addMonths(new Date(2014, 8, 1), 5)
  24. * //=> Sun Feb 01 2015 00:00:00
  25. */
  26. export default function addMonths(dirtyDate, dirtyAmount) {
  27. requiredArgs(2, arguments);
  28. var date = toDate(dirtyDate);
  29. var amount = toInteger(dirtyAmount);
  30. if (isNaN(amount)) {
  31. return new Date(NaN);
  32. }
  33. if (!amount) {
  34. // If 0 months, no-op to avoid changing times in the hour before end of DST
  35. return date;
  36. }
  37. var dayOfMonth = date.getDate(); // 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. var endOfDesiredMonth = new Date(date.getTime());
  46. endOfDesiredMonth.setMonth(date.getMonth() + amount + 1, 0);
  47. var 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(endOfDesiredMonth.getFullYear(), endOfDesiredMonth.getMonth(), dayOfMonth);
  61. return date;
  62. }
  63. }