index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 addDays
  6. * @category Day Helpers
  7. * @summary Add the specified number of days to the given date.
  8. *
  9. * @description
  10. * Add the specified number of days 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 days 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 days added
  19. * @throws {TypeError} - 2 arguments required
  20. *
  21. * @example
  22. * // Add 10 days to 1 September 2014:
  23. * const result = addDays(new Date(2014, 8, 1), 10)
  24. * //=> Thu Sep 11 2014 00:00:00
  25. */
  26. export default function addDays(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 days, no-op to avoid changing times in the hour before end of DST
  35. return date;
  36. }
  37. date.setDate(date.getDate() + amount);
  38. return date;
  39. }