Click here to Skip to main content
15,906,567 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

Excel NetworkDays for C#

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
25 Mar 2011CPOL 24K   6  
Calculate the number of whole working days between start and end with support of holidays and holiday-periods.
Excel provides the function NetworkDays[^] for the calculation of working days within a time period.

This sample uses the Time Period Library for .NET[^], to imitate this function in C#.

Additional, the sample allows to consider multiple holiday-periods:
// ----------------------------------------------------------------------
public void NetworkDaysSample()
{
  DateTime start = new DateTime( 2011, 3, 1 );
  DateTime end = new DateTime( 2011, 5, 1 );
  Console.WriteLine( "period: {0}", new CalendarTimeRange( start, end ) );
  // > period: 01.03.2011 00:00:00 - 30.04.2011 23:59:59 | 60.23:59

  Console.WriteLine( "network days without holidays: {0}", 
    NetworkDays( start, end ) );
  // > network days without holidays: 44

  // collection of holidays
  List<DateTime> holidays = new List<DateTime>(); 
  holidays.Add( new DateTime( 2011, 3, 9 ) );  // day 3/9/2011
  holidays.Add( new DateTime( 2011, 3, 16 ) ); // day 16/9/2011
  holidays.Add( new DateTime( 2011, 3, 17 ) ); // day 17/9/2011
  Console.WriteLine( "network days with holidays: {0}", 
    NetworkDays( start, end, holidays ) );
  // > network days with holidays: 41

  // collection of holiday-periods
  TimePeriodCollection holidayPeriods = new TimePeriodCollection();
  holidayPeriods.Add( new Week( 2011, 13 ) ); // w/c 13 2011
  Console.WriteLine( "network days with holidays and holiday-periods: {0}",
    NetworkDays( start, end, holidays, holidayPeriods ) );
  // > network days with holidays and holiday-periods: 36
} // NetworkDaysSample

// ----------------------------------------------------------------------
public double NetworkDays( DateTime start, DateTime end,
  IEnumerable<DateTime> holidays = null, 
  ITimePeriodCollection holidayPeriods = null )
{
  Day startDay = new Day( start < end ? start : end );
  Day endDay = new Day( end > start ? end : start );
  if ( startDay.Equals( endDay ) )
  {
    return 0;
  }

  CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();
  filter.AddWorkingWeekDays(); // only working days
  if ( holidays != null )
  {
    foreach ( DateTime holiday in holidays )
    {
      filter.ExcludePeriods.Add( new Day( holiday ) );
    }
  }
  if ( holidayPeriods != null )
  {
    filter.ExcludePeriods.AddAll( holidayPeriods );
  }

  CalendarTimeRange testPeriod = new CalendarTimeRange( start, end );
  CalendarPeriodCollector collector = 
    new CalendarPeriodCollector( filter, testPeriod );
  collector.CollectDays();

  double networkDays = 0.0;
  foreach ( ICalendarTimeRange period in collector.Periods )
  {
    networkDays += Math.Round( period.Duration.TotalDays, 2 );
  }
  return networkDays;
} // NetworkDays

License

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


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions

 
-- There are no messages in this forum --