Advertisement
lina94

CalculatingWorkdays

Aug 10th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System;
  2.  
  3. // 5. Write a method that calculates the number of workdays between today and given date, passed as parameter.
  4. // Consider that workdays are all days from Monday to Friday except a fixed list of public holidays specified preliminary as array.
  5.  
  6. class CalculatingWorkdays
  7. {
  8.     static DateTime today = DateTime.Today;
  9.     static DateTime[] holidays = new DateTime[]
  10.             {
  11.                 // Easter Holidays are not included since they change every year
  12.                 new DateTime(today.Year, 1, 1),
  13.                 new DateTime(today.Year, 3, 3),
  14.                 new DateTime(today.Year, 5, 1),
  15.                 new DateTime(today.Year, 5, 6),
  16.                 new DateTime(today.Year, 5, 24),
  17.                 new DateTime(today.Year, 9, 6),
  18.                 new DateTime(today.Year, 9, 22),
  19.                 new DateTime(today.Year, 11, 1),
  20.                 new DateTime(today.Year, 12, 24),
  21.                 new DateTime(today.Year, 12, 25),
  22.                 new DateTime(today.Year, 12, 26),
  23.             };
  24.  
  25.     static bool IsHoliday(DateTime currentDay, DateTime[] holidays)
  26.     {
  27.         foreach (var holiday in holidays)
  28.         {
  29.             if (currentDay == holiday)
  30.             {
  31.                 return true;
  32.             }
  33.         }
  34.         return false;
  35.     }
  36.  
  37.     static void ClaclulatingWorkDays(DateTime startDate, DateTime endDate, DateTime[] holidays)
  38.     {
  39.         int workDaysCounter = 0;
  40.         while (startDate < endDate)
  41.         {
  42.             if (startDate.DayOfWeek != DayOfWeek.Saturday && startDate.DayOfWeek != DayOfWeek.Sunday && !IsHoliday(startDate, holidays))
  43.             {
  44.                 workDaysCounter++;
  45.             }
  46.             startDate = startDate.AddDays(1);
  47.         }
  48.         Console.WriteLine("Work days until the end date: " + workDaysCounter);
  49.     }
  50.  
  51.     static void Main()
  52.     {
  53.         Console.WriteLine("Type an end date: ");
  54.         DateTime endDate = DateTime.Parse(Console.ReadLine());
  55.         ClaclulatingWorkDays(today, endDate, holidays);
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement