Click here to Skip to main content
13,260,383 members (43,429 online)
Click here to Skip to main content
Add your own
alternative version

Stats

3.4K views
Posted 6 Oct 2011

Excel NetworkDays for C#

, 6 Oct 2011
Rate this:
Please Sign up or sign in to vote.
public static int GetFullWorkingDaysBetween(DateTime firstDate, DateTime lastDate, IEnumerable holidays){ if (firstDate > lastDate)// Swap the dates if firstDate > lastDate { DateTime tempDate = firstDate; firstDate = lastDate; lastDate =...
public static int GetFullWorkingDaysBetween(DateTime firstDate, 
       DateTime lastDate, IEnumerable<DateTime> holidays)
{
    if (firstDate > lastDate)// Swap the dates if firstDate > lastDate
    {
        DateTime tempDate = firstDate;
        firstDate = lastDate;
        lastDate = tempDate;
    }
    int days = (int)(lastDate.Subtract(firstDate).Ticks / TimeSpan.TicksPerDay);
    int weekReminder = days % 7;
    if (weekReminder > 0)
    {
        switch (firstDate.DayOfWeek)
        {
            case DayOfWeek.Monday:
                days = days - ((weekReminder > 5) ? 1 : 0);
                // Another way for this:
                //days = days - ((int)weekReminder % 5);
                // but i think its more expensive
                break;
            case DayOfWeek.Tuesday:
                days = days - ((weekReminder > 4) ? 1 : 0) - ((weekReminder > 5) ? 1 : 0);
                // The same from above
                //days = days - ((int)weekReminder % 4);
                break;
            case DayOfWeek.Wednesday:
                days = days - ((weekReminder > 3) ? 1 : 0) - ((weekReminder > 4) ? 1 : 0);
                break;
            case DayOfWeek.Thursday:
                days = days - ((weekReminder > 2) ? 1 : 0) - ((weekReminder > 3) ? 1 : 0);
                break;
            case DayOfWeek.Friday:
                days = days - ((weekReminder > 1) ? 1 : 0) - ((weekReminder > 2) ? 1 : 0);
                break;
            case DayOfWeek.Saturday:
                days = days - 1 - ((weekReminder > 1) ? 1 : 0);
                break;
            case DayOfWeek.Sunday:
                days = days - 1;
                break;
        }
    }
    days = days - (2 * ((int)days / 7));
    if (holidays != null && holidays.Count() > 0)
    {
        foreach (DateTime holiday in holidays.Where(
                  h => h >= firstDate && h < lastDate))
        {
            DayOfWeek dayOfWeekOfHoliday = holiday.DayOfWeek;
            if (dayOfWeekOfHoliday != DayOfWeek.Saturday && 
                dayOfWeekOfHoliday != DayOfWeek.Sunday)
            {
                days = days - 1;
            }
        }
    }
    return days;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Go Cova
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171114.1 | Last Updated 6 Oct 2011
Article Copyright 2011 by Go Cova
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid