Reputation: 421
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
Here is a piece of code to precisely illustrate my question:
struct BusinessMonthRange
{
public DateTime startDate;
public DateTime endDate;
}
BusinessMonthRange GetBusinessMonthDateRange(int month, int year, List<DateTime> yearsPublicHolidays)
{
//Calculate
return BusinessMonthRange;
}
Edit: For increased clarity, I've put the assumptions in the question as pointed out by Lasse V. Karlsen and GraemeMiller in their answers
Upvotes: 1
Views: 707
Reputation: 391296
Here's a simple LINQPad program that will work out the business dates for you.
I defined a business day as:
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) };
GetBusinessMonthDateRange(12, 2013, holidays).Dump();
}
struct BusinessMonthRange
{
public DateTime startDate;
public DateTime endDate;
}
BusinessMonthRange GetBusinessMonthDateRange(int month, int year,
List<DateTime> yearsPublicHolidays)
{
DateTime startDate = new DateTime(year, month, 1);
DateTime endDate = new DateTime(year, month, 1).AddMonths(1).AddDays(-1);
while (startDate.DayOfWeek == DayOfWeek.Saturday
|| startDate.DayOfWeek == DayOfWeek.Sunday
|| yearsPublicHolidays.Contains(startDate))
{
startDate = startDate.AddDays(1);
}
while (endDate.DayOfWeek == DayOfWeek.Saturday
|| endDate.DayOfWeek == DayOfWeek.Sunday
|| yearsPublicHolidays.Contains(endDate))
{
endDate = endDate.AddDays(-1);
}
return new BusinessMonthRange { startDate = startDate, endDate = endDate };
}
The output:
Explanation:
Upvotes: 5
Reputation: 12253
Look at Itenso time period library http://www.codeproject.com/Articles/168662/Time-Period-Library-for-NET
It has functionality to solve these sort of issues. Also what is a business day. In UK it would be Mon -Fri. Other places e.g. Mid East have different days.
Itenso would let you define them and find them. Allowing you to make process generic.
Its a great library we use it a lot. So if you have other time calculation stuff worth exploring
Upvotes: 1