Advertisement
stefanpu

Working days count refactoring 2

Feb 14th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class CalculateWorkingDays
  7. {
  8.     static int currentYear = DateTime.Now.Year;
  9.     static List<DateTime> holidays = new  List<DateTime>
  10.         {
  11.            new DateTime(currentYear, 1, 1),
  12.            new DateTime(currentYear, 3, 3),
  13.            new DateTime(currentYear, 5, 1),
  14.            new DateTime(currentYear, 5, 3),
  15.            new DateTime(currentYear, 5, 6),
  16.            new DateTime(currentYear, 5, 24),
  17.            new DateTime(currentYear, 9, 22),
  18.            new DateTime(currentYear, 12, 24),
  19.            new DateTime(currentYear, 12, 25),
  20.            new DateTime(currentYear, 12, 26),
  21.            new DateTime(currentYear, 12, 31),
  22.         };
  23.     static void Main()
  24.     {
  25.         DateTime startDate = DateTime.Today;
  26.         DateTime endDate = new DateTime(currentYear, 2, 17);
  27.         CalculateWorkDays(startDate, endDate);
  28.     }
  29.  
  30.     static void CalculateWorkDays(DateTime startDate, DateTime endDate)
  31.     {
  32.  
  33.         int numberOfAllDays = (endDate - startDate).Days;
  34.  
  35.         // не е нужна
  36.         //bool isHoliday = false;
  37.         int numberOfWorkingDays = 0;
  38.  
  39.         for (int i = 0; i < numberOfAllDays; i++)
  40.         {
  41.             startDate = startDate.AddDays(1);
  42.             if (startDate.DayOfWeek != DayOfWeek.Sunday && startDate.DayOfWeek != DayOfWeek.Saturday)
  43.             {
  44.                 //for (int j = 0; j < holidays.Length; j++)
  45.                 //{
  46.                 //    if (startDate == holidays[j])
  47.                 //    {
  48.                 //        isHoliday = true;
  49.                 //        break;
  50.                 //    }
  51.                 //}
  52.                 //if (!isHoliday)
  53.                 //{
  54.                 //    numberOfWorkingDays++;
  55.                 //}
  56.                 //isHoliday = false;
  57.                 if (!holidays.Contains(startDate))
  58.                 {
  59.                     numberOfWorkingDays++;
  60.                 }
  61.  
  62.             }
  63.         }
  64.         Console.WriteLine("Number of working days:{0}",numberOfWorkingDays);
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement