index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = addDays;
  6. var _index = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  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. * ### v2.0.0 breaking changes:
  19. *
  20. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  21. *
  22. * @param {Date|Number} date - the date to be changed
  23. * @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`.
  24. * @returns {Date} - the new date with the days added
  25. * @throws {TypeError} - 2 arguments required
  26. *
  27. * @example
  28. * // Add 10 days to 1 September 2014:
  29. * const result = addDays(new Date(2014, 8, 1), 10)
  30. * //=> Thu Sep 11 2014 00:00:00
  31. */
  32. function addDays(dirtyDate, dirtyAmount) {
  33. (0, _index3.default)(2, arguments);
  34. var date = (0, _index2.default)(dirtyDate);
  35. var amount = (0, _index.default)(dirtyAmount);
  36. if (isNaN(amount)) {
  37. return new Date(NaN);
  38. }
  39. if (!amount) {
  40. // If 0 days, no-op to avoid changing times in the hour before end of DST
  41. return date;
  42. }
  43. date.setDate(date.getDate() + amount);
  44. return date;
  45. }
  46. module.exports = exports.default;