addBusinessDays.js 2.6 KB

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