index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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. * @param {Date|Number} date - the date to be changed
  13. * @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`.
  14. * @returns {Date} - the new date with the days added
  15. * @throws {TypeError} - 2 arguments required
  16. *
  17. * @example
  18. * // Add 10 days to 1 September 2014:
  19. * const result = addDays(new Date(2014, 8, 1), 10)
  20. * //=> Thu Sep 11 2014 00:00:00
  21. */
  22. export default function addDays(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 days, no-op to avoid changing times in the hour before end of DST
  31. return date;
  32. }
  33. date.setDate(date.getDate() + amount);
  34. return date;
  35. }