index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = differenceInBusinessDays;
  6. var _index = _interopRequireDefault(require("../isValid/index.js"));
  7. var _index2 = _interopRequireDefault(require("../isWeekend/index.js"));
  8. var _index3 = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index4 = _interopRequireDefault(require("../differenceInCalendarDays/index.js"));
  10. var _index5 = _interopRequireDefault(require("../addDays/index.js"));
  11. var _index6 = _interopRequireDefault(require("../isSameDay/index.js"));
  12. var _index7 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  13. var _index8 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  14. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15. /**
  16. * @name differenceInBusinessDays
  17. * @category Day Helpers
  18. * @summary Get the number of business days between the given dates.
  19. *
  20. * @description
  21. * Get the number of business day periods between the given dates.
  22. * Business days being days that arent in the weekend.
  23. * Like `differenceInCalendarDays`, the function removes the times from
  24. * the dates before calculating the difference.
  25. *
  26. * @param {Date|Number} dateLeft - the later date
  27. * @param {Date|Number} dateRight - the earlier date
  28. * @returns {Number} the number of business days
  29. * @throws {TypeError} 2 arguments required
  30. *
  31. * @example
  32. * // How many business days are between
  33. * // 10 January 2014 and 20 July 2014?
  34. * var result = differenceInBusinessDays(
  35. * new Date(2014, 6, 20),
  36. * new Date(2014, 0, 10)
  37. * )
  38. * //=> 136
  39. */
  40. function differenceInBusinessDays(dirtyDateLeft, dirtyDateRight) {
  41. (0, _index8.default)(2, arguments);
  42. var dateLeft = (0, _index3.default)(dirtyDateLeft);
  43. var dateRight = (0, _index3.default)(dirtyDateRight);
  44. if (!(0, _index.default)(dateLeft) || !(0, _index.default)(dateRight)) return NaN;
  45. var calendarDifference = (0, _index4.default)(dateLeft, dateRight);
  46. var sign = calendarDifference < 0 ? -1 : 1;
  47. var weeks = (0, _index7.default)(calendarDifference / 7);
  48. var result = weeks * 5;
  49. dateRight = (0, _index5.default)(dateRight, weeks * 7); // the loop below will run at most 6 times to account for the remaining days that don't makeup a full week
  50. while (!(0, _index6.default)(dateLeft, dateRight)) {
  51. // sign is used to account for both negative and positive differences
  52. result += (0, _index2.default)(dateRight) ? 0 : sign;
  53. dateRight = (0, _index5.default)(dateRight, sign);
  54. }
  55. return result === 0 ? 0 : result;
  56. }
  57. module.exports = exports.default;