addDays.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. "use strict";
  2. exports.addDays = addDays;
  3. var _index = require("./toDate.js");
  4. var _index2 = require("./constructFrom.js");
  5. /**
  6. * @name addDays
  7. * @category Day Helpers
  8. * @summary Add the specified number of days to the given date.
  9. *
  10. * @description
  11. * Add the specified number of days to the given date.
  12. *
  13. * @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).
  14. *
  15. * @param date - The date to be changed
  16. * @param amount - The amount of days to be added.
  17. *
  18. * @returns The new date with the days added
  19. *
  20. * @example
  21. * // Add 10 days to 1 September 2014:
  22. * const result = addDays(new Date(2014, 8, 1), 10)
  23. * //=> Thu Sep 11 2014 00:00:00
  24. */
  25. function addDays(date, amount) {
  26. const _date = (0, _index.toDate)(date);
  27. if (isNaN(amount)) return (0, _index2.constructFrom)(date, NaN);
  28. if (!amount) {
  29. // If 0 days, no-op to avoid changing times in the hour before end of DST
  30. return _date;
  31. }
  32. _date.setDate(_date.getDate() + amount);
  33. return _date;
  34. }