Alex Rivera | Logout

Number of days in date range, excluding weekends and other dates, in C#

Asked 2011-06-30T16:10:39.770
9

I have a C# method like this:

public static int DaysLeft(DateTime startDate, DateTime endDate, Boolean excludeWeekends, String excludeDates)
{
}

What it's supposed to do is calculate the number of days between the startDate and endDate, but optionally needs to exclude weekends and also other dates (passed in as a comma-separated string of dates).

I have absolutely no idea how to tackle this. My gut instinct would be to loop from startDate to endDate and do some string comparisons, but from what I can find out, C# doesn't allow looping through dates in that way - or at least it's not a very elegant way of doing things.

Edit
Report

1 Answer

0

Here's pseudocode for a different approach which I've used in SQL:

Find the total number of days between the two dates
Subtract number of weekends
Remove a day if the start date is a sunday
Remove a day if the start date is a saturday
Remove any other days you don't want (see my comment above for how to do this in c#)

answered 2011-06-30T16:27:32.437

Your Answer