Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a simple <a href="http://linqpad.net" rel="nofollow noreferrer">LINQPad</a> program that will work out the business dates for you.</p> <p>I defined a business day as:</p> <ul> <li>Not Saturday</li> <li>Not Sunday</li> <li>Not one of the dates in the list of holidays</li> </ul> <p>Note that these rules might be subject to local and cultural variations, some places in the world, Saturday would be a business day.</p> <p>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.</p> <hr> <p><strong>Assumption</strong>: 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.</p> <p>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".</p> <p>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.</p> <hr> <p>Anyway, here's the code:</p> <pre><code>void Main() { var holidays = new List&lt;DateTime&gt; { 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&lt;DateTime&gt; 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 }; } </code></pre> <p>The output:</p> <p><img src="https://i.stack.imgur.com/5hgEN.png" alt="LINQPad output"></p> <p>Explanation:</p> <ul> <li>The 1st of December 2013 is a Sunday, hence it starts at the 2nd of December.</li> <li>The 31st of December is a Tuesday, but I made it a holiday by including that date in the list of public holidays for 2013, and thus Monday 30th was chosen as the end instead.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload