Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- // 5. Write a method that calculates the number of workdays between today and given date, passed as parameter.
- // Consider that workdays are all days from Monday to Friday except a fixed list of public holidays specified preliminary as array.
- class CalculatingWorkdays
- {
- static DateTime today = DateTime.Today;
- static DateTime[] holidays = new DateTime[]
- {
- // Easter Holidays are not included since they change every year
- new DateTime(today.Year, 1, 1),
- new DateTime(today.Year, 3, 3),
- new DateTime(today.Year, 5, 1),
- new DateTime(today.Year, 5, 6),
- new DateTime(today.Year, 5, 24),
- new DateTime(today.Year, 9, 6),
- new DateTime(today.Year, 9, 22),
- new DateTime(today.Year, 11, 1),
- new DateTime(today.Year, 12, 24),
- new DateTime(today.Year, 12, 25),
- new DateTime(today.Year, 12, 26),
- };
- static bool IsHoliday(DateTime currentDay, DateTime[] holidays)
- {
- foreach (var holiday in holidays)
- {
- if (currentDay == holiday)
- {
- return true;
- }
- }
- return false;
- }
- static void ClaclulatingWorkDays(DateTime startDate, DateTime endDate, DateTime[] holidays)
- {
- int workDaysCounter = 0;
- while (startDate < endDate)
- {
- if (startDate.DayOfWeek != DayOfWeek.Saturday && startDate.DayOfWeek != DayOfWeek.Sunday && !IsHoliday(startDate, holidays))
- {
- workDaysCounter++;
- }
- startDate = startDate.AddDays(1);
- }
- Console.WriteLine("Work days until the end date: " + workDaysCounter);
- }
- static void Main()
- {
- Console.WriteLine("Type an end date: ");
- DateTime endDate = DateTime.Parse(Console.ReadLine());
- ClaclulatingWorkDays(today, endDate, holidays);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement