Advertisement
Guest User

AddBusinessDays

a guest
Feb 21st, 2020
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. /* Extension */
  2. public static DateTime AddBusinessDays(this DateTime current, int days, IEnumerable<DateTime> holidays = null)
  3. {
  4. var sign = Math.Sign(days); // if (days == 0) return 0 , if (days > 0) return 1 , if (days < 0) return -1
  5. var unsignedDays = Math.Abs(days);
  6. for (var i = 0; i < unsignedDays; i++)
  7. {
  8. do
  9. {
  10. current = current.AddDays(sign);
  11. }
  12. while (current.DayOfWeek == DayOfWeek.Saturday
  13. || current.DayOfWeek == DayOfWeek.Sunday
  14. || (holidays != null && holidays.Contains(current.Date))
  15. );
  16. }
  17. return current;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement