Click here to Skip to main content
15,886,095 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Calculate Business Hours

Rate me:
Please Sign up or sign in to vote.
4.95/5 (8 votes)
13 Apr 2012CPOL 42.7K   22   20
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)


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

 
QuestionMessage Closed Pin
15-Jan-21 6:54
Maggiewill15-Jan-21 6:54 
Questionneed help about calculations method Pin
marco.laporta4-Jun-17 9:56
marco.laporta4-Jun-17 9:56 
AnswerRe: need help about calculations method Pin
Jani Giannoudis5-Jun-17 22:28
Jani Giannoudis5-Jun-17 22:28 
QuestionCalculate Business Hours Pin
ThomaLuke7-May-17 6:26
professionalThomaLuke7-May-17 6:26 
AnswerRe: Calculate Business Hours Pin
Jani Giannoudis30-May-17 0:18
Jani 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
María Clara Fleitas Pereyra15-Dec-14 7:04 
QuestionBusiness hours Pin
chinswain1-May-14 3:15
chinswain1-May-14 3:15 
AnswerRe: Business hours Pin
Jani Giannoudis5-May-14 8:26
Jani Giannoudis5-May-14 8:26 
QuestionCalculating business hours Pin
bc2m4-Apr-12 4:49
professionalbc2m4-Apr-12 4:49 
AnswerRe: Calculating business hours Pin
Jani Giannoudis4-Apr-12 20:57
Jani Giannoudis4-Apr-12 20:57 
GeneralRe: Calculating business hours Pin
bc2m5-Apr-12 5:28
professionalbc2m5-Apr-12 5:28 
AnswerRe: Calculating business hours Pin
Jani Giannoudis5-Apr-12 5:39
Jani Giannoudis5-Apr-12 5:39 
GeneralRe: Calculating business hours Pin
bc2m5-Apr-12 8:32
professionalbc2m5-Apr-12 8:32 
GeneralRe: Calculating business hours Pin
alaamh11-Apr-12 20:53
alaamh11-Apr-12 20:53 
AnswerRe: Calculating business hours Pin
Jani Giannoudis11-Apr-12 22:08
Jani Giannoudis11-Apr-12 22:08 
GeneralOne last question: finding start date/time... Pin
bc2m16-Apr-12 3:37
professionalbc2m16-Apr-12 3:37 
GeneralRe: Is this a business day? Pin
bc2m16-Apr-12 5:48
professionalbc2m16-Apr-12 5:48 
AnswerRe: Is this a business day? Pin
Jani Giannoudis16-Apr-12 21:30
Jani Giannoudis16-Apr-12 21:30 
GeneralRe: Is this a business day? Pin
bc2m17-Apr-12 23:02
professionalbc2m17-Apr-12 23:02 
GeneralRe: Calculating business hours Pin
bc2m16-Apr-12 5:59
professionalbc2m16-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.