Click here to Skip to main content
11,714,345 members (83,967 online)
Click here to Skip to main content

Calculate Business Hours

, 13 Apr 2012 CPOL 18.3K 20
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.

You may also be interested in...

Comments and Discussions

 
QuestionBusiness hours Pin
María Clara Fleitas Pereyra15-Dec-14 7:04
memberMaría Clara Fleitas Pereyra15-Dec-14 7:04 
QuestionBusiness hours Pin
chinswain1-May-14 3:15
memberchinswain1-May-14 3:15 
AnswerRe: Business hours Pin
Jani Giannoudis5-May-14 8:26
memberJani Giannoudis5-May-14 8:26 
QuestionCalculating business hours Pin
bc2m4-Apr-12 4:49
memberbc2m4-Apr-12 4:49 
AnswerRe: Calculating business hours Pin
Jani Giannoudis4-Apr-12 20:57
mvpJani Giannoudis4-Apr-12 20:57 
GeneralRe: Calculating business hours Pin
bc2m5-Apr-12 5:28
memberbc2m5-Apr-12 5:28 
AnswerRe: Calculating business hours Pin
Jani Giannoudis5-Apr-12 5:39
mvpJani Giannoudis5-Apr-12 5:39 
GeneralRe: Calculating business hours Pin
bc2m5-Apr-12 8:32
memberbc2m5-Apr-12 8:32 
GeneralRe: Calculating business hours Pin
alaamh11-Apr-12 20:53
memberalaamh11-Apr-12 20:53 
AnswerRe: Calculating business hours Pin
Jani Giannoudis11-Apr-12 22:08
mvpJani Giannoudis11-Apr-12 22:08 
GeneralOne last question: finding start date/time... Pin
bc2m16-Apr-12 3:37
memberbc2m16-Apr-12 3:37 
GeneralRe: Is this a business day? Pin
bc2m16-Apr-12 5:48
memberbc2m16-Apr-12 5:48 
AnswerRe: Is this a business day? Pin
Jani Giannoudis16-Apr-12 21:30
mvpJani Giannoudis16-Apr-12 21:30 
GeneralRe: Is this a business day? Pin
bc2m17-Apr-12 23:02
memberbc2m17-Apr-12 23:02 
GeneralRe: Calculating business hours Pin
bc2m16-Apr-12 5:59
memberbc2m16-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 | Terms of Use | Mobile
Web03 | 2.8.150819.1 | Last Updated 13 Apr 2012
Article Copyright 2012 by Jani Giannoudis
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid