differenceInBusinessDays.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. exports.differenceInBusinessDays = differenceInBusinessDays;
  3. var _index = require("./addDays.js");
  4. var _index2 = require("./differenceInCalendarDays.js");
  5. var _index3 = require("./isSameDay.js");
  6. var _index4 = require("./isValid.js");
  7. var _index5 = require("./isWeekend.js");
  8. var _index6 = require("./toDate.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. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  21. *
  22. * @param dateLeft - The later date
  23. * @param dateRight - The earlier date
  24. *
  25. * @returns The number of business days
  26. *
  27. * @example
  28. * // How many business days are between
  29. * // 10 January 2014 and 20 July 2014?
  30. * const result = differenceInBusinessDays(
  31. * new Date(2014, 6, 20),
  32. * new Date(2014, 0, 10)
  33. * )
  34. * //=> 136
  35. *
  36. * // How many business days are between
  37. * // 30 November 2021 and 1 November 2021?
  38. * const result = differenceInBusinessDays(
  39. * new Date(2021, 10, 30),
  40. * new Date(2021, 10, 1)
  41. * )
  42. * //=> 21
  43. *
  44. * // How many business days are between
  45. * // 1 November 2021 and 1 December 2021?
  46. * const result = differenceInBusinessDays(
  47. * new Date(2021, 10, 1),
  48. * new Date(2021, 11, 1)
  49. * )
  50. * //=> -22
  51. *
  52. * // How many business days are between
  53. * // 1 November 2021 and 1 November 2021 ?
  54. * const result = differenceInBusinessDays(
  55. * new Date(2021, 10, 1),
  56. * new Date(2021, 10, 1)
  57. * )
  58. * //=> 0
  59. */
  60. function differenceInBusinessDays(dateLeft, dateRight) {
  61. const _dateLeft = (0, _index6.toDate)(dateLeft);
  62. let _dateRight = (0, _index6.toDate)(dateRight);
  63. if (!(0, _index4.isValid)(_dateLeft) || !(0, _index4.isValid)(_dateRight))
  64. return NaN;
  65. const calendarDifference = (0, _index2.differenceInCalendarDays)(
  66. _dateLeft,
  67. _dateRight,
  68. );
  69. const sign = calendarDifference < 0 ? -1 : 1;
  70. const weeks = Math.trunc(calendarDifference / 7);
  71. let result = weeks * 5;
  72. _dateRight = (0, _index.addDays)(_dateRight, weeks * 7);
  73. // the loop below will run at most 6 times to account for the remaining days that don't makeup a full week
  74. while (!(0, _index3.isSameDay)(_dateLeft, _dateRight)) {
  75. // sign is used to account for both negative and positive differences
  76. result += (0, _index5.isWeekend)(_dateRight) ? 0 : sign;
  77. _dateRight = (0, _index.addDays)(_dateRight, sign);
  78. }
  79. // Prevent negative zero
  80. return result === 0 ? 0 : result;
  81. }