Advertisement
Pazzobg

[Objects and Classes]Count working days

Jun 12th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. namespace _01.CountWorkingDays
  2. {
  3.     using System;
  4.     using System.Globalization;
  5.  
  6.     public class CountWorkingDays
  7.     {
  8.         public static void Main()
  9.         {
  10.             DateTime startDate = DateTime.ParseExact(Console.ReadLine(), "dd-MM-yyyy", CultureInfo.InvariantCulture);
  11.             DateTime endDate = DateTime.ParseExact(Console.ReadLine(), "dd-MM-yyyy", CultureInfo.InvariantCulture);
  12.  
  13.             DateTime[] officialHolidays = AddOfficialHolidays();
  14.  
  15.             int workingDaysCounter = 0;
  16.  
  17.             for (DateTime currentDate = startDate; currentDate <= endDate; currentDate = currentDate.AddDays(1))
  18.             {
  19.                 if (currentDate.DayOfWeek == DayOfWeek.Saturday || currentDate.DayOfWeek == DayOfWeek.Sunday)
  20.                 {
  21.                     continue;
  22.                 }
  23.  
  24.                 bool isHoliday = false;
  25.  
  26.                 for (int i = 0; i < officialHolidays.Length; i++)
  27.                 {
  28.                     DateTime tempHolidayCheck = officialHolidays[i];
  29.  
  30.                     if (currentDate.Day == tempHolidayCheck.Day && currentDate.Month == tempHolidayCheck.Month)
  31.                     {
  32.                         isHoliday = true;
  33.                     }
  34.                 }
  35.  
  36.                 if (!isHoliday)
  37.                 {
  38.                     workingDaysCounter++;
  39.                 }
  40.             }
  41.  
  42.             Console.WriteLine(workingDaysCounter);
  43.         }
  44.  
  45.         public static DateTime[] AddOfficialHolidays()
  46.         {
  47.             DateTime[] officialHolidaysArray = new DateTime[]
  48.             {
  49.                 new DateTime (2015, 1, 1),
  50.                 new DateTime (2015, 3, 3),
  51.                 new DateTime (2015, 5, 1),
  52.                 new DateTime (2015, 5, 6),
  53.                 new DateTime (2015, 5, 24),
  54.                 new DateTime (2015, 9, 6),
  55.                 new DateTime (2015, 9, 22),
  56.                 new DateTime (2015, 11, 1),
  57.                 new DateTime (2015, 12, 24),
  58.                 new DateTime (2015, 12, 25),
  59.                 new DateTime (2015, 12, 26)
  60.             };
  61.  
  62.             return officialHolidaysArray;
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement