Calculate the number of business hours between two dates.

M

Mufasa

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.
 
I

Ignacio Machin ( .NET/ C# MVP )

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.

Hi,

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

If you add holidays it gets more difficult htough :)
 
J

Jeroen Mostert

Mufasa said:
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.
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.
 
P

parez

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.

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++;
}
}
 
G

Guillermo Grillo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top