addBusinessDays.cjs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. exports.addBusinessDays = addBusinessDays;
  3. var _index = require("./constructFrom.cjs");
  4. var _index2 = require("./isSaturday.cjs");
  5. var _index3 = require("./isSunday.cjs");
  6. var _index4 = require("./isWeekend.cjs");
  7. var _index5 = require("./toDate.cjs");
  8. /**
  9. * The {@link addBusinessDays} function options.
  10. */
  11. /**
  12. * @name addBusinessDays
  13. * @category Day Helpers
  14. * @summary Add the specified number of business days (mon - fri) to the given date.
  15. *
  16. * @description
  17. * Add the specified number of business days (mon - fri) to the given date, ignoring weekends.
  18. *
  19. * @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).
  20. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  21. *
  22. * @param date - The date to be changed
  23. * @param amount - The amount of business days to be added.
  24. * @param options - An object with options
  25. *
  26. * @returns The new date with the business days added
  27. *
  28. * @example
  29. * // Add 10 business days to 1 September 2014:
  30. * const result = addBusinessDays(new Date(2014, 8, 1), 10)
  31. * //=> Mon Sep 15 2014 00:00:00 (skipped weekend days)
  32. */
  33. function addBusinessDays(date, amount, options) {
  34. const _date = (0, _index5.toDate)(date, options?.in);
  35. const startedOnWeekend = (0, _index4.isWeekend)(_date, options);
  36. if (isNaN(amount)) return (0, _index.constructFrom)(options?.in, NaN);
  37. const hours = _date.getHours();
  38. const sign = amount < 0 ? -1 : 1;
  39. const fullWeeks = Math.trunc(amount / 5);
  40. _date.setDate(_date.getDate() + fullWeeks * 7);
  41. // Get remaining days not part of a full week
  42. let restDays = Math.abs(amount % 5);
  43. // Loops over remaining days
  44. while (restDays > 0) {
  45. _date.setDate(_date.getDate() + sign);
  46. if (!(0, _index4.isWeekend)(_date, options)) restDays -= 1;
  47. }
  48. // If the date is a weekend day and we reduce a dividable of
  49. // 5 from it, we land on a weekend date.
  50. // To counter this, we add days accordingly to land on the next business day
  51. if (
  52. startedOnWeekend &&
  53. (0, _index4.isWeekend)(_date, options) &&
  54. amount !== 0
  55. ) {
  56. // If we're reducing days, we want to add days until we land on a weekday
  57. // If we're adding days we want to reduce days until we land on a weekday
  58. if ((0, _index2.isSaturday)(_date, options))
  59. _date.setDate(_date.getDate() + (sign < 0 ? 2 : -1));
  60. if ((0, _index3.isSunday)(_date, options))
  61. _date.setDate(_date.getDate() + (sign < 0 ? 1 : -2));
  62. }
  63. // Restore hours to avoid DST lag
  64. _date.setHours(hours);
  65. return _date;
  66. }