What would be the best way to determine the first and last business days of a given month

What would be the best way to determine the first and last business days of a given month along with a given set of yearly public holidays?

Assuming

• a Business Day is one that is not among Saturdays, Sundays or given public holidays
• a Business Month starts with, and ends at a Business Day as defined above.

Here is a piece of code to precisely illustrate my question:

``````struct BusinessMonthRange
{
public DateTime startDate;
public DateTime endDate;
}

{
//Calculate
}
```
```

Edit: For increased clarity, I've put the assumptions in the question as pointed out by Lasse V. Karlsen and GraemeMiller in their answers

Here's a simple LINQPad program that will work out the business dates for you.

I defined a business day as:

• Not Saturday
• Not Sunday
• Not one of the dates in the list of holidays

Note that these rules might be subject to local and cultural variations, some places in the world, Saturday would be a business day.

It works by starting at the first and last day of the specified month, and working inwards from both directions until it finds business days in both ends. In other words, if the last day of the month is not a business day, let's try the previous day, and so on, until we hit a business day.

Assumption: I'm assuming that "business month" is not exactly like a "business year", because a "business year" might actually span dates from two calendar years.

What I mean is, if a business month should always start on a Monday, and have rules like "if the first week of the calendar month has more than half the dates in the specified month, but starts the previous month, the business month also starts in the previous calendar month".

For instance, with such a rule, the business month of October 2013 would start on the 30th of September, the Monday of that week. I'm assuming this is not how you want the business month calculated, because if you do, the below code will not do that.

Anyway, here's the code:

``````void Main()
{
var holidays = new List<DateTime> { new DateTime(2013, 12, 31) };
}

{
public DateTime startDate;
public DateTime endDate;
}

List<DateTime> yearsPublicHolidays)
{
DateTime startDate = new DateTime(year, month, 1);
while (startDate.DayOfWeek == DayOfWeek.Saturday
|| startDate.DayOfWeek == DayOfWeek.Sunday
|| yearsPublicHolidays.Contains(startDate))
{
}
while (endDate.DayOfWeek == DayOfWeek.Saturday
|| endDate.DayOfWeek == DayOfWeek.Sunday
|| yearsPublicHolidays.Contains(endDate))
{