index.js 2.7 KB

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