Click here to Skip to main content
Click here to Skip to main content
Alternative Tip
Go to top

Calculate Business Hours

, 13 Apr 2012
Rate this:
Please Sign up or sign in to vote.
Calculation of business hours (including holidays) using the Time Period Library for .NET
Calculation of business hours (including holidays) using the Time Period Library for .NET[^]:
// ----------------------------------------------------------------------
public void CalculateBusinessHoursSample()
{
  CalendarTimeRange testPeriod = new CalendarTimeRange( 
    new DateTime( 2011, 3, 1 ), new DateTime( 2011, 5, 1 ) );
  Console.WriteLine( "period: {0}", testPeriod );
  // > period: 01.03.2011 00:00:00 - 30.04.2011 23:59:59 | 60.23:59

  TimePeriodCollection holidays = new TimePeriodCollection();
  Console.WriteLine( "business hours without holidays: {0}", 
    CalculateBusinessHours( testPeriod, holidays ) );
  // > business hours without holidays: 396

  holidays.Add( new Day( 2011, 3, 9 ) );       // day 3/9/2011
  Console.WriteLine( "business hours with holidays: {0}", 
    CalculateBusinessHours( testPeriod, holidays ) );
  // > business hours with holidays: 387

  holidays.Add( new Days( 2011, 3, 16, 2 ) );  // days 16/9/2011 and 17/9/2011
  Console.WriteLine( "business hours with more holidays: {0}", 
    CalculateBusinessHours( testPeriod, holidays ) );
  // > business hours with more holidays: 369

  holidays.Add( new Week( 2011, 13 ) );        // w/c 13 2011
  Console.WriteLine( "business hours with even more holidays: {0}", 
    CalculateBusinessHours( testPeriod, holidays ) );
  // > business hours with even more holidays: 324
} // CalculateBusinessHoursSample

// ----------------------------------------------------------------------
public double CalculateBusinessHours( CalendarTimeRange testPeriod, ITimePeriodCollection holidays = null )
{
  CalendarPeriodCollectorFilter filter = new CalendarPeriodCollectorFilter();
  filter.AddWorkingWeekDays(); // only working days
  filter.CollectingHours.Add( new HourRange( 8, 12 ) );  // opening hours morning
  filter.CollectingHours.Add( new HourRange( 13, 18 ) ); // opening hours afternoon
  if ( holidays != null )
  {
    filter.ExcludePeriods.AddAll( holidays );
  }
 
  CalendarPeriodCollector collector = new CalendarPeriodCollector( 
    filter, testPeriod );
  collector.CollectHours();
 
  double businessHours = 0.0;
  foreach ( ICalendarTimeRange period in collector.Periods )
  {
    businessHours += Math.Round( period.Duration.TotalHours, 2 );
  }
  return businessHours;
} // CalculateBusinessHours

License

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

Share

About the Author

Jani Giannoudis
Software Developer (Senior)
Switzerland Switzerland
Jani is Co-founder of Meerazo.com, a free service which allows to share resources like locations, things, persons and their services in a cooperating group of people.

Comments and Discussions

 
QuestionBusiness hours Pinmemberchinswain1-May-14 3:15 
AnswerRe: Business hours PinmemberJani Giannoudis5-May-14 8:26 
QuestionCalculating business hours Pinmemberbc2m4-Apr-12 4:49 
AnswerRe: Calculating business hours PinmvpJani Giannoudis4-Apr-12 20:57 
GeneralRe: Calculating business hours Pinmemberbc2m5-Apr-12 5:28 
AnswerRe: Calculating business hours PinmvpJani Giannoudis5-Apr-12 5:39 
GeneralRe: Calculating business hours Pinmemberbc2m5-Apr-12 8:32 
GeneralRe: Calculating business hours Pinmemberalaamh11-Apr-12 20:53 
AnswerRe: Calculating business hours PinmvpJani Giannoudis11-Apr-12 22:08 
GeneralOne last question: finding start date/time... [modified] Pinmemberbc2m16-Apr-12 3:37 
GeneralRe: Is this a business day? [modified] Pinmemberbc2m16-Apr-12 5:48 
AnswerRe: Is this a business day? PinmvpJani Giannoudis16-Apr-12 21:30 
GeneralRe: Is this a business day? Pinmemberbc2m17-Apr-12 23:02 
GeneralRe: Calculating business hours Pinmemberbc2m16-Apr-12 5:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 13 Apr 2012
Article Copyright 2012 by Jani Giannoudis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid