Click here to Skip to main content
13,053,971 members (64,350 online)
Click here to Skip to main content
Add your own
alternative version

Stats

26.4K views
21 bookmarked
Posted 13 Apr 2012

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
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionneed help about calculations method Pin
marco.laporta4-Jun-17 9:56
membermarco.laporta4-Jun-17 9:56 
AnswerRe: need help about calculations method Pin
Jani Giannoudis5-Jun-17 22:28
memberJani Giannoudis5-Jun-17 22:28 
QuestionCalculate Business Hours Pin
thanhluan1987cntt7-May-17 6:26
professionalthanhluan1987cntt7-May-17 6:26 
AnswerRe: Calculate Business Hours Pin
Jani Giannoudis30-May-17 0:18
memberJani Giannoudis30-May-17 0:18 
GeneralRe: Calculate Business Hours Pin
ThomaLuke10-Jun-17 4:59
professionalThomaLuke10-Jun-17 4:59 
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    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 13 Apr 2012
Article Copyright 2012 by Jani Giannoudis
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid