Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calculate the number of business hours between two dates.

62 views
Skip to first unread message

Mufasa

unread,
Apr 10, 2008, 12:00:36 PM4/10/08
to
Anybody have a function that will calculate the number of business hours
between two dates? I will assume eight hours a day and don't want to count
weekends.

TIA - Jeff.


Ignacio Machin ( .NET/ C# MVP )

unread,
Apr 10, 2008, 1:13:01 PM4/10/08
to

Hi,

Not that I know of, but it wound be rocket science to build one.

If you add holidays it gets more difficult htough :)

Jeroen Mostert

unread,
Apr 10, 2008, 2:24:38 PM4/10/08
to
Well, then, here's close to half the solution:

int countBusinessHours(int days) {
return days * 8;
}

The other function is left as an exercise to the reader. The DateTime class
has everything you need.

--
J.

parez

unread,
Apr 10, 2008, 2:35:38 PM4/10/08
to

Heres the rest. .i think..


DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now;
TimeSpan s = d2.Subtract(d1);

DateTime nd;

int businessdayCounter = 0;
for (int i = 0; i < s.Days; i++)
{
nd = d1.AddDays(i);

if (nd.DayOfWeek == DayOfWeek.Saturday || nd.DayOfWeek
== DayOfWeek.Sunday)
{
// ignore
}
else
{
businessdayCounter++;
}
}

Guillermo Grillo

unread,
Aug 25, 2010, 12:00:16 PM8/25/10
to
Hi Jeff,

I need to program a function to determine an expiration datetime from a specific datetime, for instance NOW.

The problem that I have using Datetime class is that I cannot find the way to set the workday in 8 hours. The rest of the function works very well, but the expiration datetime is calculated using 24-hours day.

I would like your advice on how can I solve this problem.

Thanks, Guillermo from Costa Rica

> On Thursday, April 10, 2008 12:00 PM Mufasa wrote:

> Anybody have a function that will calculate the number of business hours

> between two dates? I will assume eight hours a day and do not want to count
> weekends.
>
> TIA - Jeff.


>> On Thursday, April 10, 2008 2:24 PM Jeroen Mostert wrote:

>> Mufasa wrote:
>> Well, then, here's close to half the solution:
>>
>> int countBusinessHours(int days) {
>> return days * 8;
>> }
>>
>> The other function is left as an exercise to the reader. The DateTime class
>> has everything you need.
>>
>> --
>> J.


>>> On Saturday, April 12, 2008 8:34 AM Ignacio Machin ( .NET/ C# MVP ) wrote:

>>> Hi,
>>>
>>> Not that I know of, but it wound be rocket science to build one.
>>>
>>> If you add holidays it gets more difficult htough :)


>>>>> On Monday, July 05, 2010 3:29 AM Todd Wilder wrote:

>>>>> I blogged about calculating total hours, holiday hours working hours and non-working hours here:
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> http://www.spindepth.com/2010/06/calculating-business-hours-in.html
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Basically its too difficult to do all this with calculated columns, so I have a content editor web part that has a button that uses javascript to update a list's calculations, kind of like a refresh button. Its not exactly what you want, but its close to what you want.


>>>>> Submitted via EggHeadCafe - Software Developer Portal of Choice
>>>>> A Comparison of Managed Compression Algorithms
>>>>> http://www.eggheadcafe.com/tutorials/aspnet/71485ecc-2d2d-435a-9c35-3d12b279f9ae/a-comparison-of-managed-compression-algorithms.aspx

Peter Duniho

unread,
Aug 25, 2010, 4:38:46 PM8/25/10
to
Guillermo Grillo wrote:
> Hi Jeff,
>
> I need to program a function to determine an expiration datetime from a specific datetime, for instance NOW.
>
> The problem that I have using Datetime class is that I cannot find the way to set the workday in 8 hours. The rest of the function works very well, but the expiration datetime is calculated using 24-hours day.
>
> I would like your advice on how can I solve this problem.

There seems to be quite a lot of good information in the (very old)
thread you quoted in your own question. Perhaps you could be more
specific. Why is that information not enough to help you?

As for the basic idea: it's true, this is not built into the DateTime
class, nor AFAIK any other .NET class. But it's not that hard to
accomplish. One straightforward solution is to simply have a loop in
which you subtract the amount of time for a given business day, for each
business day until you run out of time. When the current time left
reaches zero, the business day you're on is the you want.

Pete

0 new messages