Guest User

Untitled

a guest
Jan 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApplication1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. for (int i = 13; i <= 31; i++)
  10. {
  11. DateTime start = new DateTime(2014, 10, i);
  12. DateTime stop = new DateTime(2014, 10, 31);
  13.  
  14. int totalWorkingDays = GetNumberOfWorkingDays(start, stop);
  15.  
  16. Console.WriteLine("There are {1} working days from Oct {0}, 2014 to Oct 31, 2014.", i, totalWorkingDays);
  17. }
  18. }
  19.  
  20. private static int GetNumberOfWorkingDays(DateTime start, DateTime stop)
  21. {
  22. TimeSpan interval = stop - start;
  23.  
  24. int totalWeek = interval.Days / 7;
  25. int totalWorkingDays = 5 * totalWeek;
  26.  
  27. int remainingDays = interval.Days % 7;
  28.  
  29.  
  30. for (int i = 0; i <= remainingDays; i++)
  31. {
  32. DayOfWeek test = (DayOfWeek)(((int)start.DayOfWeek + i) % 7);
  33. if (test >= DayOfWeek.Monday && test <= DayOfWeek.Friday)
  34. totalWorkingDays++;
  35. }
  36.  
  37. return totalWorkingDays;
  38. }
  39. }
  40. }
  41.  
  42. private static int GetNumberOfWorkingDaysJeroen(DateTime start, DateTime stop)
  43. {
  44. int days = 0;
  45. while(start <= stop)
  46. {
  47. if(start.DayOfWeek != DayOfWeek.Saturday && start.DayOfWeek != DayOfWeek.Sunday)
  48. {
  49. ++days;
  50. }
  51. start = start.AddDays(1);
  52. }
  53. return days;
  54. }
  55.  
  56. private static int GetNumberOfWorkingDays(DateTime start, DateTime stop)
  57. {
  58. var days = (stop - start).Days + 1;
  59. return workDaysInFullWeeks(days) + workDaysInPartialWeek(start.DayOfWeek, days);
  60. }
  61.  
  62. private static int workDaysInFullWeeks(int totalDays)
  63. {
  64. return (totalDays / 7) * 5;
  65. }
  66.  
  67. private static int workDaysInPartialWeek(DayOfWeek firstDay, int totalDays)
  68. {
  69. var remainingDays = totalDays % 7;
  70. var daysToSaturday = (int) DayOfWeek.Saturday - (int) firstDay;
  71. if(remainingDays <= daysToSaturday)
  72. return remainingDays;
  73. /* daysToSaturday are the days before the weekend,
  74. * the rest of the expression computes the days remaining after we
  75. * ignore Saturday and Sunday
  76. */
  77. // Range ends in a Saturday or in a Sunday
  78. if (remainingDays <= daysToSaturday + 2)
  79. return daysToSaturday;
  80. // Range ends after a Sunday
  81. else
  82. return remainingDays - 2;
  83. }
  84.  
  85. namespace Extensions{
  86. using System;
  87. public static class DateTimeExtensions {
  88. public static int GetNumberOfWeekDays(this DateTime dateTime, DateTime end_date_time){
  89. int days = 0;
  90. while(dateTime <= end_date_time) {
  91. if(dateTime.DayOfWeek != DayOfWeek.Saturday && // IF not sat
  92. dateTime.DayOfWeek != DayOfWeek.Sunday) // And not sunday
  93. ++days;
  94. dateTime = dateTime.AddDays(1);
  95. }
  96. return days;
  97. }
  98.  
  99. public static DateTime AddWeekDays(this DateTime dateTime, int days) =>
  100. dateTime.AddDays(dateTime.GetNumberOfWeekDays(dateTime.AddDays(days)));
  101. }
  102. }
  103.  
  104. namespace ConsoleApplication1 {
  105. using System;
  106. using static Extensions.DateTimeExtensions;
  107. class Program {
  108. static void Main(string[] args) {
  109.  
  110. DateTime date = DateTime.Now;
  111.  
  112. // Example usage of Get Number of weekdays
  113. int weekDays = date.GetNumberOfWeekDays(dateTime.AddDays(10))
  114.  
  115. // Example usage of Add Week Days
  116. DateTime endDate = date.AddWeekDays(4);
  117.  
  118. }
  119. }
  120. }
  121.  
  122. public int GetWorkingDays(DateTime from, DateTime to)
  123. {
  124. var totalDays = 0;
  125. for (var date = from; date < to; date = date.AddDays(1))
  126. {
  127. if (date.DayOfWeek != DayOfWeek.Saturday
  128. && date.DayOfWeek != DayOfWeek.Sunday)
  129. totalDays++;
  130. }
  131.  
  132. return totalDays;
  133. }
Add Comment
Please, Sign In to add comment