How do you subtract Business days in C# based on this for Adding Business days:
public static DateTime AddBusinessDays(DateTime date, int days = 5)
{
if (days < 0)
{
throw new ArgumentException("days cannot be negative", "days");
}
if (days == 0) return date;
if (date.DayOfWeek == DayOfWeek.Saturday)
{
date = date.AddDays(2);
days -= 1;
}
else if (date.DayOfWeek == DayOfWeek.Sunday)
{
date = date.AddDays(1);
days -= 1;
}
date = date.AddDays(days / 5 * 7);
int extraDays = days % 5;
if ((int)date.DayOfWeek + extraDays > 5)
{
extraDays += 2;
}
return date.AddDays(extraDays);
}
That cannot take negative numbers, so need another one specifically for subtracting business days.
Edit: That "duplicate" question is measuring the difference between two dates with business days. I just want a starting date, and amount of days to subtract to come up with the result. This to be done as a function, not as a extension, as you see above for AddDays.
And a method without loops as you see above would be the most efficient.