1

I want to calculate the next business date for a given month and date.

For example:

I'll give the month 'January' and date '1'. I want to get the result of date is `Ddecember' and '31' (in the following year) . Please help me to calculate it.

Jeff B
  • 8,572
  • 17
  • 61
  • 140
Fernando
  • 358
  • 3
  • 8
  • 21
  • 3
    How can the next business date be a date before the date you give as parameter? Shouldn't it be 2. January (for 2012)? – Øyvind Bråthen Feb 20 '12 at 12:03
  • 1
    I have to say I don't understand what you are doing here... Why is the next business date for 1st January nearly two years later (31 december in the next year) or did you get these two things the wrong way round)? If its next working day (ie monday to friday) then you'll ned the year as well at the very least... – Chris Feb 20 '12 at 12:04
  • 1
    Also, you you only want to avoid saturdays and sundays, or other holidays like christmas and easter and so forth as well? – Øyvind Bråthen Feb 20 '12 at 12:04
  • 1
    Fernando, please clarify your question some. – Greg B Feb 20 '12 at 12:05
  • i just calculate this date only not holiday and others.. – Fernando Feb 20 '12 at 12:12

2 Answers2

4

Try:

var startDate = ...;
while (startDate.DayOfWeek == DayOfWeek.Saturday || startDate.DayOfWeek == DayOfWeek.Sunday)
    startDate = startDate.AddDays(1);

Next year's previous business day:

var startDate = ...;
startDate = startDate.AddYears(1);
while (startDate.DayOfWeek == DayOfWeek.Saturday || startDate.DayOfWeek == DayOfWeek.Sunday)
    startDate = startDate.AddDays(-1);
Pavel Krymets
  • 6,253
  • 1
  • 22
  • 35
3

What constitutes a business day? If anything bar Sat/Sunday you can use an extension method such as Calculate the number of business days between two dates?

However in most countries you would also ignore bank holidays which usually means that you want to have a table/list of those as well and ensure you ignore them. So you can have a method to evaluate both of these conditions.

Think about your rules, including going back to the previous business day rather than forward to the next applicable business day. Once you have the set of rules you can define functions to evaluate candidate business days plus navigating between days to find the right result.

Community
  • 1
  • 1
kaj
  • 5,133
  • 2
  • 21
  • 18