AZ. - 1 year ago 73
C# Question

I need to find 2 elegant complete implementations of

``````public static DateTime AddBusinessDays(this DateTime date, int days)
{
// code here
}

and

public static int GetBusinessDays(this DateTime start, DateTime end)
{
// code here
}
``````

O(1) preferable (no loops).

EDIT:
By business days i mean working days (Monday, Tuesday, Wednesday, Thursday, Friday). No holidays, just weekends excluded.

I already have some ugly solutions that seem to work but i wonder if there are elegant ways to do this. Thanks

This is what i've written so far. It works in all cases and does negatives too.

``````public static DateTime AddBusinessDays(this DateTime startDate,
{
if(direction == 1)
{
if(startDate.DayOfWeek == DayOfWeek.Saturday)
{
}
else if(startDate.DayOfWeek == DayOfWeek.Sunday)
{
}
}
else
{
if(startDate.DayOfWeek == DayOfWeek.Saturday)
{
}
else if(startDate.DayOfWeek == DayOfWeek.Sunday)
{
}
}

int initialDayOfWeek = Convert.ToInt32(startDate.DayOfWeek);

int weeksBase = Math.Abs(businessDays / 5);

if((direction == 1 && addDays + initialDayOfWeek > 5) ||
(direction == -1 && addDays >= initialDayOfWeek))
{
}

int totalDays = (weeksBase * 7) + addDays;
}
``````

Latest attempt for your first function:

``````public static DateTime AddBusinessDays(DateTime date, int days)
{
if (days < 0)
{
throw new ArgumentException("days cannot be negative", "days");
}

if (days == 0) return date;

if (date.DayOfWeek == DayOfWeek.Saturday)
{
days -= 1;
}
else if (date.DayOfWeek == DayOfWeek.Sunday)
{
days -= 1;
}

date = date.AddDays(days / 5 * 7);
int extraDays = days % 5;

if ((int)date.DayOfWeek + extraDays > 5)
{
}

}
``````

The second function, GetBusinessDays, can be implemented as follows:

``````public static int GetBusinessDays(DateTime start, DateTime end)
{
if (start.DayOfWeek == DayOfWeek.Saturday)
{
}
else if (start.DayOfWeek == DayOfWeek.Sunday)
{
}

if (end.DayOfWeek == DayOfWeek.Saturday)
{
}
else if (end.DayOfWeek == DayOfWeek.Sunday)
{
}

int diff = (int)end.Subtract(start).TotalDays;

int result = diff / 7 * 5 + diff % 7;

if (end.DayOfWeek < start.DayOfWeek)
{
return result - 2;
}
else{
return result;
}
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download