We help IT Professionals succeed at work.

Get next business day from date

TechMonster
TechMonster used Ask the Experts™
on
Hello,

Hopefully this one won't be too hard.   Looking to get next business day from date.  So if I have
Sat or Sun to give me date format 07/06/2010
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

DateTime dt=DateTime.Parse("12-01-2010");
double dadd = 1;
if (dt.DayOfWeek.ToString() == "Sunday") dadd++;
if (dt.DayOfWeek.ToString() == "Saturday") dadd++;
dt.AddDays(dt);
Sorry, should be:
 dt.AddDays(dadd);
Daniel Van Der WerkenIndependent Consultant

Commented:
Here is a method that is simple and works:



public static DateTime GetNextBusinessDay( DateTime day )
        {
            DateTime nextBusinessDay = DateTime.MinValue;

            DateTime tomorrow = day.AddDays( 1d );

            switch ( tomorrow.DayOfWeek )
            {
                case DayOfWeek.Monday:
                case DayOfWeek.Tuesday:
                case DayOfWeek.Wednesday:
                case DayOfWeek.Thursday:
                case DayOfWeek.Friday:
                {
                    nextBusinessDay = tomorrow;
                    break;
                }
                case DayOfWeek.Saturday:
                {
                    nextBusinessDay = tomorrow.AddDays( 2 );
                    break;
                }
                case DayOfWeek.Sunday:
                {
                    nextBusinessDay = tomorrow.AddDays( 1 );
                    break;
                }
                default:
                {
                    break;
                }
            }

            return nextBusinessDay;
        }

Open in new window

Dan, unless I am reading this wrong, in your example, it is going to return 00:00:00.0000000, January 1, 0001 if tomorrows dayofweek is Monday-Thursday.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
I wouldn't use a string literal to check the day as it might not work in different cultures...

You could also do:


        private DateTime NextBusinessDay(DateTime dt)
        {
            dt = dt.AddDays(1); // get tomorrow
            while (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
            {
                dt = dt.AddDays(1); // keep moving forward until Monday is reached
            }
            return dt.Date;
        }

This also doesn't take into account if it is super early in the day and the "business day" hasn't even begun yet.  What if the current time is 03:00 am on Monday?  Should the next day still be Tuesday...or should it be Monday since no one is even at work yet?  You could pass in a TimeSpan that indicates when the work day begins and make a decision based on that too.  Depends on what you're doing I suppose...  =)
Daniel Van Der WerkenIndependent Consultant

Commented:
rpoole69:

It'll return DateTime.MinValue if the switch statement goes to the default case, which it never should.  Therefore, in some unforeseen error situation, you can check for a problem.  The individual case statements for the days for tomorrow for Monday - Friday all do the same thing.

Regardless, here is the output for this method:

Today is Tuesday, July 06, 2010 and the next business day is Wednesday, July 07, 2010
Today is Wednesday, July 07, 2010 and the next business day is Thursday, July 08, 2010
Today is Thursday, July 08, 2010 and the next business day is Friday, July 09, 2010
Today is Friday, July 09, 2010 and the next business day is Monday, July 12, 2010
Today is Saturday, July 10, 2010 and the next business day is Monday, July 12, 2010
Today is Sunday, July 11, 2010 and the next business day is Monday, July 12, 2010
Today is Monday, July 12, 2010 and the next business day is Tuesday, July 13, 2010
Today is Tuesday, July 13, 2010 and the next business day is Wednesday, July 14, 2010

I used the code in the code box below to test it.  Yes, I like Winforms.  I suppose I could've used a loop, but copy and paste works.

There are probably a hundred ways to do this well.  I'm just providing one and it might not even be the best.

            DateTime today = DateTime.Now;
            DateTime nextBusinessDay = GetNextBusinessDay( today );
            textBoxOutput.Text += "Today is " + today.ToLongDateString() + " and the next business day is " + nextBusinessDay.ToLongDateString() + Environment.NewLine;
            today = today.AddDays( 1 );
            nextBusinessDay = GetNextBusinessDay( today );
            textBoxOutput.Text += "Today is " + today.ToLongDateString() + " and the next business day is " + nextBusinessDay.ToLongDateString() + Environment.NewLine;
            today = today.AddDays( 1 );
            nextBusinessDay = GetNextBusinessDay( today );
            textBoxOutput.Text += "Today is " + today.ToLongDateString() + " and the next business day is " + nextBusinessDay.ToLongDateString() + Environment.NewLine;
            today = today.AddDays( 1 );
            nextBusinessDay = GetNextBusinessDay( today );
            textBoxOutput.Text += "Today is " + today.ToLongDateString() + " and the next business day is " + nextBusinessDay.ToLongDateString() + Environment.NewLine;
            today = today.AddDays( 1 );
            nextBusinessDay = GetNextBusinessDay( today );
            textBoxOutput.Text += "Today is " + today.ToLongDateString() + " and the next business day is " + nextBusinessDay.ToLongDateString() + Environment.NewLine;
            today = today.AddDays( 1 );
            nextBusinessDay = GetNextBusinessDay( today );
            textBoxOutput.Text += "Today is " + today.ToLongDateString() + " and the next business day is " + nextBusinessDay.ToLongDateString() + Environment.NewLine;
            today = today.AddDays( 1 );
            nextBusinessDay = GetNextBusinessDay( today );
            textBoxOutput.Text += "Today is " + today.ToLongDateString() + " and the next business day is " + nextBusinessDay.ToLongDateString() + Environment.NewLine;
            today = today.AddDays( 1 );
            nextBusinessDay = GetNextBusinessDay( today );
            textBoxOutput.Text += "Today is " + today.ToLongDateString() + " and the next business day is " + nextBusinessDay.ToLongDateString() + Environment.NewLine;

Open in new window

Ahh, I was reading it wrong, didn't notice the cases before Friday did not have a break in them :)
Shahan AyyubSenior Software Engineer

Commented:
try like this:(not tested)

        private DateTime NextBusinessDay(DateTime dt)
        {
         DateTime NextDate = dt;
         NextDate = NextDate.AddDays(Math.Abs((int)dt.DayOfWeek-(int)DayOfWeek.Sunday));
         return NextDate.Date;
        }

Since not tested, I am not sure that what is the number of sunday and what is the number of Monday.But, I hope it will help you. I can test it tonight.
 
 
Shahan AyyubSenior Software Engineer

Commented:
You can try like this:

I am taking Monday" as a Starting Week Day. If you want to change it SUNDAY then:

Change this:
string[] days = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday", "Sunday"};

with this:

string[] days = new string[] { "Sunday","Monday","Tuesday", "Wednesday", "Thursday","Friday", "Saturday"};

i.e., start array 'days' from that day.


private DateTime GetNextBusinessDay(DateTime dt)
{
    string[] days = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday", "Sunday"};
    List<string> l=new List<string>(days);
    return dt.AddDays(7-l.IndexOf(dt.DayOfWeek.ToString())).Date;
}

Open in new window

Author

Commented:
Thanks everyone for your response.  
Here is the solution I used:

If Weekday(DateCHK) = 1 Then
                    DateCHK = DateAdd(DateInterval.Day, 1, DateCHK)
                               
ElseIf Weekday(DateCHK) = 7 Then
                    DateCHK = DateAdd(DateInterval.Day, 2, DateCHK)
                 
             Else
End If

Author

Commented:
It seems there are multiple solutions to this problem and I am sure they all will work but I  accepted the first response just because it was first.

Thanks again