index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = addDays;
  7. var _index = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  8. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. /**
  11. * @name addDays
  12. * @category Day Helpers
  13. * @summary Add the specified number of days to the given date.
  14. *
  15. * @description
  16. * Add the specified number of days to the given date.
  17. *
  18. * @param {Date|Number} date - the date to be changed
  19. * @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`.
  20. * @returns {Date} - the new date with the days added
  21. * @throws {TypeError} - 2 arguments required
  22. *
  23. * @example
  24. * // Add 10 days to 1 September 2014:
  25. * const result = addDays(new Date(2014, 8, 1), 10)
  26. * //=> Thu Sep 11 2014 00:00:00
  27. */
  28. function addDays(dirtyDate, dirtyAmount) {
  29. (0, _index3.default)(2, arguments);
  30. var date = (0, _index2.default)(dirtyDate);
  31. var amount = (0, _index.default)(dirtyAmount);
  32. if (isNaN(amount)) {
  33. return new Date(NaN);
  34. }
  35. if (!amount) {
  36. // If 0 days, no-op to avoid changing times in the hour before end of DST
  37. return date;
  38. }
  39. date.setDate(date.getDate() + amount);
  40. return date;
  41. }
  42. module.exports = exports.default;