Advertisement
Teodor92

NumberOfWorkDays

Jan 16th, 2013
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. /* Write a method that calculates the number of workdays between
  2.  * today and given date, passed as parameter. Consider that workdays
  3.  * are all days from Monday to Friday except a fixed
  4.  * list of public holidays specified preliminary as array.
  5.  */
  6.  
  7. using System;
  8.  
  9. public class NumberOfWorkDays
  10. {
  11.     public static void Main()
  12.     {
  13.         // input
  14.         Console.WriteLine("Enter a end date in YYYY/MM/DD format");
  15.         string[] endDateStr = Console.ReadLine().Split('/');
  16.         int day = int.Parse(endDateStr[2]);
  17.         int month = int.Parse(endDateStr[1]);
  18.         int year = int.Parse(endDateStr[0]);
  19.  
  20.         // main logic
  21.         DateTime startDay = DateTime.Today;
  22.         DateTime endDay = new DateTime(year, month, day);
  23.         int timeLen = 0;
  24.         timeLen = Math.Abs((endDay - startDay).Days);
  25.         if (startDay > endDay)
  26.         {
  27.             startDay = endDay;
  28.             endDay = DateTime.Today;
  29.         }
  30.  
  31.         // Holydays
  32.         DateTime[] holidays =
  33.         {
  34.             new DateTime(2013, 1, 1),
  35.             new DateTime(2012, 2, 2),
  36.             new DateTime(2012, 3, 3),
  37.             new DateTime(2012, 4, 4),
  38.             new DateTime(2013, 1, 18)
  39.         };
  40.         Console.WriteLine(timeLen);
  41.         int workDayCounter = 0;
  42.         bool isHoliday = false;
  43.  
  44.         // Day checker
  45.         for (int i = 0; i < timeLen; i++)
  46.         {
  47.             startDay = startDay.AddDays(1);
  48.             if (startDay.DayOfWeek != DayOfWeek.Sunday && startDay.DayOfWeek != DayOfWeek.Saturday)
  49.             {
  50.                 for (int j = 0; j < holidays.Length; j++)
  51.                 {
  52.                     if (startDay == holidays[j])
  53.                     {
  54.                         isHoliday = true;
  55.                         break;
  56.                     }
  57.                 }
  58.  
  59.                 if (!isHoliday)
  60.                 {
  61.                     workDayCounter++;
  62.                 }
  63.  
  64.                 isHoliday = false;
  65.             }
  66.         }
  67.  
  68.         Console.WriteLine(workDayCounter);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement