Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

44 строки
1.6 KiB

  1. import isWeekend from '../isWeekend/index.js';
  2. import toDate from '../toDate/index.js';
  3. import toInteger from '../_lib/toInteger/index.js';
  4. import requiredArgs from '../_lib/requiredArgs/index.js';
  5. /**
  6. * @name addBusinessDays
  7. * @category Day Helpers
  8. * @summary Add the specified number of business days (mon - fri) to the given date.
  9. *
  10. * @description
  11. * Add the specified number of business days (mon - fri) to the given date, ignoring weekends.
  12. *
  13. * @param {Date|Number} date - the date to be changed
  14. * @param {Number} amount - the amount of business days to be added. Positive decimals will be rounded using `Math.floor`, decimals less than zero will be rounded using `Math.ceil`.
  15. * @returns {Date} the new date with the business days added
  16. * @throws {TypeError} 2 arguments required
  17. *
  18. * @example
  19. * // Add 10 business days to 1 September 2014:
  20. * var result = addBusinessDays(new Date(2014, 8, 1), 10)
  21. * //=> Mon Sep 15 2014 00:00:00 (skipped weekend days)
  22. */
  23. export default function addBusinessDays(dirtyDate, dirtyAmount) {
  24. requiredArgs(2, arguments);
  25. var date = toDate(dirtyDate);
  26. var amount = toInteger(dirtyAmount);
  27. if (isNaN(amount)) return new Date(NaN);
  28. var hours = date.getHours();
  29. var sign = amount < 0 ? -1 : 1;
  30. var fullWeeks = toInteger(amount / 5);
  31. date.setDate(date.getDate() + fullWeeks * 7); // Get remaining days not part of a full week
  32. var restDays = Math.abs(amount % 5); // Loops over remaining days
  33. while (restDays > 0) {
  34. date.setDate(date.getDate() + sign);
  35. if (!isWeekend(date)) restDays -= 1;
  36. } // Restore hours to avoid DST lag
  37. date.setHours(hours);
  38. return date;
  39. }