Calculate time within office hours

Hi,

I’m trying to build a function which calculates an expiration date for a document, within this time the document must be processed by another person.

The background is this: someone creates a new document, which has to be done in e.g. 4 hours. At the time, the document is saved, a field “to be done until” shall be filled with this value.

My problem is: this function should use only the the normal office-time [8-17h].

How can I calculate, if the expiration time is in the office-hours - and if not transfer the rest of the time to the next working day and put this date/time into the field?

Does anyone has an idea?

Thanks,

Thomas Seeb

Subject: Calculate time within office hours

When I get stuck with this type of problem, I tend to start putting things down on paper. Draw out a daily time line, put a dot at hour(x), see what happens when you add 4 hours and then start playing with equations to make the system do what you want.

Think it through logically - if now + 4 > 17, then how many hours do I need to add to push the time to where it belongs tomorrow?

Write it out - how would you know what to do if it was 13h?

Weekends will be a bit more complex but not too bad. There are a lot of postings in the 4/5 forum on how to skip weekends - search a bit over there.

Holidays - way more complex but do-able. You’ll need a way to define days that are holidays and need your code to see if one is coming up tomorrow then add (enough hours to skip the day).

This is one you should be able to figure out on your own - basic math…

If I misunderstood the question and you’re asking something else, let me know.

HTH

Doug

Subject: RE: Calculate time within office hours

I just put it down on paper in little steps and now it doesn’t seem to be so difficult any longer.Thanks Graham and Doug - your answers were very helpful!

-Thomas

Subject: Calculate time within office hours

Generally, I would do the following.

Given a Starting Date and Starting Time, plus a Delta Time.

Determine if the Delta Time is greater than a business day.

If so, determine how many days that is, and increment the Starting Date appropriately, and remove them from the Delta time

Determine if the Delta Time plus the Starting Time goes past the end of the business day

If so, increment the Starting Date and remove one business day from the Starting Time.
(i.e. If there was one hour left in the Starting time, set it to one hour before the start of the next business day)

Add the Delta Time to the Starting time

Note - when incrementing the Starting Date, you will want to take weekends into account.

e.g.

Starting Date = 2006/1/6 (a Friday)

Starting Time = 4pm

Business Start Time = 8am

Business End Time = 5am

Business Hours (End - Start) = 11 hours

Delta Time = 25 hours

Delta Time = 2 business days (22h) plus 3 hours

Increment Starting Date (with weekends) to 2006/1/10

Set Delta Time to (25-22) 3h

Starting Time + Delta Time > Business End, so

Increment Starting Date to 2006/1/11

Subtract Busness Hours from Start Hours (4pm - 11h = 7am)

Add Delta Hours to Starting Hours (7am + 3h = 10am)