Dec 20

Adding business hours….

Part of our workflows have some time critical tasks. In this particular instance, there is a measurable KPI of 12 business hours to respond to an event.

The DateTime construct is very useful for date manipulation, but does not appear to provide an easy way to understand Business Hours. So how to go about tracking this?

The simplest way that I managed to come up with, was to create two methods, One that checks if a given DateTime exists on a business day, the other that checks whether the time falls within business hours, named onBusinessDay() and inBusinessHours() respecitively.

static bool onBusinessDay(DateTime timeToTest)
ArrayList mBusinessDays = new ArrayList();

if (mBusinessDays.Contains(timeToTest.DayOfWeek.ToString()))
return true;
return false;



static bool inBusinessHours(DateTime timeToTest)
DateTime businessStart = DateTime.Parse(timeToTest.ToShortDateString() + " 09:00:00");
DateTime businessEnd = DateTime.Parse(timeToTest.ToShortDateString() + " 17:30:00");
if ((timeToTest.CompareTo(businessStart) >= 0) && (timeToTest.CompareTo(businessEnd) <= 0)) { return true; } return false; }

This returns the value's nicely, but now we have the problem, How do we add the hours and track the business hours accuratley? Remember, DateTime has no concept of business hours.

As part of a learning exercise, I decided to use an Enum to create a Bit Flag set to track the BusinessHours and BusinessDay results. (Remember to declare this outside of your main method.

enum DateHoursCheck : uint
inBusinessHours = 0x01, //Bit 0
onBusinessDay = 0x02, //Bit 1
notUsed1 = 0x04, //Bit 2
notUsed2 = 0x08 //Bit 3

And finally, we get to the program code itself. In order to track the business hours usage, we just create a For loop that increments Int i until it reaches taskHours - 1, The key here, is that we only increment i, if the hour added results in the testDate being inBusinessHours AND on a businessDay. This way we work around out of hours and weekend issues.

static void Main(string[] args)
DateTime startDate;
DateTime testDate;
int taskHours = 12;

DateHoursCheck dhc = new DateHoursCheck();

startDate = DateTime.Now;
testDate = startDate;

//We're going to surround the logic in a big loop construct.
//This will set the task time and then test it for business day and business hours.
//Ifboth are correct, we'll end up with True True, which will end the loop.

for (int i = 0; i < taskHours-1;) { //Each time the enum is 11, then we'll inc i. //This should mean we check each of the 12 business hours. //Reset the bit flags to 0 dhc = 0; testDate = testDate.AddHours(1); if(inBusinessHours(testDate)) { dhc = dhc | DateHoursCheck.inBusinessHours; } if(onBusinessDay(testDate)) { dhc = dhc | DateHoursCheck.onBusinessDay; } if (((dhc & DateHoursCheck.onBusinessDay) == DateHoursCheck.onBusinessDay) && ((dhc & DateHoursCheck.inBusinessHours) == DateHoursCheck.inBusinessHours)) { i++; } } Console.WriteLine("New Task Time - " + testDate.ToString()); Console.ReadLine(); }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


This site uses Akismet to reduce spam. Learn how your comment data is processed.