addDays.cjs 1.3 KB

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