index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = addBusinessDays;
  6. var _index = _interopRequireDefault(require("../isWeekend/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. var _index5 = _interopRequireDefault(require("../isSunday/index.js"));
  11. var _index6 = _interopRequireDefault(require("../isSaturday/index.js"));
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * @name addBusinessDays
  15. * @category Day Helpers
  16. * @summary Add the specified number of business days (mon - fri) to the given date.
  17. *
  18. * @description
  19. * Add the specified number of business days (mon - fri) to the given date, ignoring weekends.
  20. *
  21. * @param {Date|Number} date - the date to be changed
  22. * @param {Number} amount - the amount of business days to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
  23. * @returns {Date} the new date with the business days added
  24. * @throws {TypeError} 2 arguments required
  25. *
  26. * @example
  27. * // Add 10 business days to 1 September 2014:
  28. * const result = addBusinessDays(new Date(2014, 8, 1), 10)
  29. * //=> Mon Sep 15 2014 00:00:00 (skipped weekend days)
  30. */
  31. function addBusinessDays(dirtyDate, dirtyAmount) {
  32. (0, _index4.default)(2, arguments);
  33. var date = (0, _index2.default)(dirtyDate);
  34. var startedOnWeekend = (0, _index.default)(date);
  35. var amount = (0, _index3.default)(dirtyAmount);
  36. if (isNaN(amount)) return new Date(NaN);
  37. var hours = date.getHours();
  38. var sign = amount < 0 ? -1 : 1;
  39. var fullWeeks = (0, _index3.default)(amount / 5);
  40. date.setDate(date.getDate() + fullWeeks * 7); // Get remaining days not part of a full week
  41. var restDays = Math.abs(amount % 5); // Loops over remaining days
  42. while (restDays > 0) {
  43. date.setDate(date.getDate() + sign);
  44. if (!(0, _index.default)(date)) restDays -= 1;
  45. } // If the date is a weekend day and we reduce a dividable of
  46. // 5 from it, we land on a weekend date.
  47. // To counter this, we add days accordingly to land on the next business day
  48. if (startedOnWeekend && (0, _index.default)(date) && amount !== 0) {
  49. // If we're reducing days, we want to add days until we land on a weekday
  50. // If we're adding days we want to reduce days until we land on a weekday
  51. if ((0, _index6.default)(date)) date.setDate(date.getDate() + (sign < 0 ? 2 : -1));
  52. if ((0, _index5.default)(date)) date.setDate(date.getDate() + (sign < 0 ? 1 : -2));
  53. } // Restore hours to avoid DST lag
  54. date.setHours(hours);
  55. return date;
  56. }
  57. module.exports = exports.default;