fork(1) download
  1. using static System.Console;
  2. using System;
  3.  
  4. public class Program {
  5. public static void Main() {
  6. WriteLine(AddBusinessDays(DateTime.Now, 8));
  7. WriteLine(AddBusinessDays(new DateTime(2015, 10, 26), 15));
  8. }
  9.  
  10. public static DateTime AddBusinessDays(DateTime date, int days) {
  11. if (days < 0) throw new ArgumentException("days cannot be negative", "days");
  12. if (days == 0) return date;
  13. if (date.DayOfWeek == DayOfWeek.Saturday) {
  14. date = date.AddDays(2);
  15. days -= 1;
  16. } else if (date.DayOfWeek == DayOfWeek.Sunday) {
  17. date = date.AddDays(1);
  18. days -= 1;
  19. }
  20. date = date.AddDays(days / 5 * 7);
  21. int extraDays = days % 5;
  22. if ((int)date.DayOfWeek + extraDays > 5) extraDays += 2;
  23. return date.AddDays(extraDays);
  24. }
  25. }
  26.  
  27. //https://pt.stackoverflow.com/q/94496/101
Success #stdin #stdout 0.04s 17756KB
stdin
Standard input is empty
stdout
11/4/2019 5:44:26 PM
11/16/2015 12:00:00 AM