fork download
  1. using System;
  2.  
  3. public class Program {
  4. public static void Main() {
  5. Console.WriteLine((new DateTime(2016, 06, 13)).BusinessDaysUntil(DateTime.Now));
  6. }
  7. }
  8. public static class DateExt {
  9. public static int BusinessDaysUntil(this DateTime firstDay, DateTime lastDay, params DateTime[] bankHolidays) {
  10. firstDay = firstDay.Date;
  11. lastDay = lastDay.Date;
  12. if (firstDay > lastDay) throw new ArgumentException("Incorrect last day " + lastDay);
  13. TimeSpan span = lastDay - firstDay;
  14. int businessDays = span.Days + 1;
  15. int fullWeekCount = businessDays / 7;
  16. // find out if there are weekends during the time exceedng the full weeks
  17. if (businessDays > fullWeekCount*7) {
  18. // we are here to find out if there is a 1-day or 2-days weekend
  19. // in the time interval remaining after subtracting the complete weeks
  20. int firstDayOfWeek = (int) firstDay.DayOfWeek;
  21. int lastDayOfWeek = (int) lastDay.DayOfWeek;
  22. if (lastDayOfWeek < firstDayOfWeek)
  23. lastDayOfWeek += 7;
  24. if (firstDayOfWeek <= 6) {
  25. if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
  26. businessDays -= 2;
  27. else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
  28. businessDays -= 1;
  29. }
  30. else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
  31. businessDays -= 1;
  32. }
  33. // subtract the weekends during the full weeks in the interval
  34. businessDays -= fullWeekCount + fullWeekCount;
  35. // subtract the number of bank holidays during the time interval
  36. foreach (DateTime bankHoliday in bankHolidays) {
  37. DateTime bh = bankHoliday.Date;
  38. if (firstDay <= bh && bh <= lastDay) --businessDays;
  39. }
  40. return businessDays;
  41. }
  42. }
  43.  
  44. //https://pt.stackoverflow.com/q/134961/101
Success #stdin #stdout 0.03s 17876KB
stdin
Standard input is empty
stdout
1144