Hi,
I tried searching this and did not get exactly what I needed. I want to add 2 Business days to a given day. If it is Monday on 09/24/2007 10:00:00 then the code has to add 2 days to become 09/26/2007 10:00:00.
However, if it is Friday on 09/28/2007 10:00:00, then the code has to add 2 business days to become 10/02/2007 10:00:00.
Please help.
Thanks,
Prasanna.
AlanPosted Sep 27, 2007, 4:04 PM
This should do it:
using System;
class Program
{
static void Main()
{
string startDate1 = "09/24/2007 10:00:00";
string endDate1 = AddTwoBusinessDays(startDate1);
Console.WriteLine("{0} plus 2 business days is {1}", startDate1, endDate1);
string startDate2 = "09/28/2007 10:00:00";
string endDate2 = AddTwoBusinessDays(startDate2);
Console.WriteLine("{0} plus 2 business days is {1}", startDate2, endDate2);
Console.ReadKey();
}
public static string AddTwoBusinessDays(string startDate)
{
string format = "MM/dd/yyyy HH:mm:ss";
DateTime dt = DateTime.ParseExact(startDate, format, null);
switch (dt.DayOfWeek)
{
case DayOfWeek.Thursday:
case DayOfWeek.Friday:
return dt.AddDays(4).ToString(format);
case DayOfWeek.Saturday:
return dt.AddDays(3).ToString(format);
default:
return dt.AddDays(2).ToString(format);
}
}
}
EDITED: To simplify switch statement