index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import addDays from "../addDays/index.js";
  2. import differenceInCalendarDays from "../differenceInCalendarDays/index.js";
  3. import isSameDay from "../isSameDay/index.js";
  4. import isValid from "../isValid/index.js";
  5. import isWeekend from "../isWeekend/index.js";
  6. import toDate from "../toDate/index.js";
  7. import requiredArgs from "../_lib/requiredArgs/index.js";
  8. import toInteger from "../_lib/toInteger/index.js";
  9. /**
  10. * @name differenceInBusinessDays
  11. * @category Day Helpers
  12. * @summary Get the number of business days between the given dates.
  13. *
  14. * @description
  15. * Get the number of business day periods between the given dates.
  16. * Business days being days that arent in the weekend.
  17. * Like `differenceInCalendarDays`, the function removes the times from
  18. * the dates before calculating the difference.
  19. *
  20. * @param {Date|Number} dateLeft - the later date
  21. * @param {Date|Number} dateRight - the earlier date
  22. * @returns {Number} the number of business days
  23. * @throws {TypeError} 2 arguments required
  24. *
  25. * @example
  26. * // How many business days are between
  27. * // 10 January 2014 and 20 July 2014?
  28. * const result = differenceInBusinessDays(
  29. * new Date(2014, 6, 20),
  30. * new Date(2014, 0, 10)
  31. * )
  32. * //=> 136
  33. *
  34. * // How many business days are between
  35. * // 30 November 2021 and 1 November 2021?
  36. * const result = differenceInBusinessDays(
  37. * new Date(2021, 10, 30),
  38. * new Date(2021, 10, 1)
  39. * )
  40. * //=> 21
  41. *
  42. * // How many business days are between
  43. * // 1 November 2021 and 1 December 2021?
  44. * const result = differenceInBusinessDays(
  45. * new Date(2021, 10, 1),
  46. * new Date(2021, 11, 1)
  47. * )
  48. * //=> -22
  49. *
  50. * // How many business days are between
  51. * // 1 November 2021 and 1 November 2021 ?
  52. * const result = differenceInBusinessDays(
  53. * new Date(2021, 10, 1),
  54. * new Date(2021, 10, 1)
  55. * )
  56. * //=> 0
  57. */
  58. export default function differenceInBusinessDays(dirtyDateLeft, dirtyDateRight) {
  59. requiredArgs(2, arguments);
  60. var dateLeft = toDate(dirtyDateLeft);
  61. var dateRight = toDate(dirtyDateRight);
  62. if (!isValid(dateLeft) || !isValid(dateRight)) return NaN;
  63. var calendarDifference = differenceInCalendarDays(dateLeft, dateRight);
  64. var sign = calendarDifference < 0 ? -1 : 1;
  65. var weeks = toInteger(calendarDifference / 7);
  66. var result = weeks * 5;
  67. dateRight = addDays(dateRight, weeks * 7);
  68. // the loop below will run at most 6 times to account for the remaining days that don't makeup a full week
  69. while (!isSameDay(dateLeft, dateRight)) {
  70. // sign is used to account for both negative and positive differences
  71. result += isWeekend(dateRight) ? 0 : sign;
  72. dateRight = addDays(dateRight, sign);
  73. }
  74. return result === 0 ? 0 : result;
  75. }