1

I am developing c#.net solution where i have to calculate Start Date based on provided End date and Duration (back days) without weekends.

i.e. End Date: 05/5/2011 Back days: 5

Start Date = (05/5/2011) - 5 days (Excludes weekends)

Start Date = 29/04/2011

thanks you,

Zeon05
  • 13
  • 3
  • See `AddBusinessDays()` in [this answer](http://stackoverflow.com/questions/1044688/add-business-days-and-getbusinessdays/1044821#1044821) and do `AddBusinessDays(endDate, -5)` or use [Fluent DateTime](http://fluentdatetime.codeplex.com/) which has a similar method and a lot more in it. – Bala R May 05 '11 at 01:22
  • To clarify (05/5/2011) - 5 = (28/04/2011), right? not 29/04/2011. Want to make sure if this is a requirement or a typo. – Alex Aza May 05 '11 at 04:20

3 Answers3

0

Something like this is probably what I'd do:

DateTime CalcStartDate(DateTime endTime, int daysBack)
{
    DateTime startTime = endTime.Date;

    while (daysBack > 0)
    {
        startTime = startTime.AddDays(-1);
        if (startTime.DayOfWeek != DayOfWeek.Saturday && startTime.DayOfWeek != DayOfWeek.Sunday)
        {
            --daysBack;
        }
    }

    return startTime;
}

Or even better, Bala's suggestion of using a library. Date and time is messy, a hardened/tested library is usually a good choice.

Matt Greer
  • 60,826
  • 17
  • 123
  • 123
  • Thanks all for sharing their idea about the issue – Zeon05 May 05 '11 at 04:56
  • I tried this and it's works for me.. public DateTime CalcStartDate(DateTime EndDate, int daysBack) { DateTime StartDate = EndDate; for (int i = 1; i < daysBack; i++) { if (StartDate.DayOfWeek == DayOfWeek.Sunday) { StartDate = StartDate.AddDays(-3); } else { StartDate = StartDate.AddDays(-1); } } return StartDate; } – Zeon05 May 05 '11 at 05:05
0

Bala R has the answer correctly. Here is a link to an article on how to use the AddBusinessDays() method:

http://www.codeproject.com/KB/cs/AddBusinessDay.aspx

IAmTimCorey
  • 16,412
  • 5
  • 39
  • 75
0

I don't know how far back you are going to go. If it is a lot of days back then looping through the days might be a little CPU intensive. Well, probably not with modern processors...

I decided to implement a solution without a loop. My code is a little more difficult to read, but it should be more efficient performance-wise.

public static class DateTimeExtensions
{
    public static DateTime SubtractBusinessDays(this DateTime fromDateTime, int days)
    {
        var subtractDays = days % 5;
        var dayNumber = fromDateTime.DayOfWeek == DayOfWeek.Sunday ? 7 : (int)fromDateTime.DayOfWeek;
        var addDays = Math.Max(dayNumber - 5, 0);
        var result = fromDateTime.AddDays(addDays - subtractDays - (days / 5 * 7));
        if ((addDays + dayNumber) % 7 <= subtractDays)
            result = result.AddDays(-2);
        return result;
    }
}
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • Thanks all for sharing your idea on this issue.. I taken bit of logic from Matt's answer and rewrite as below which meet my requirement.. – Zeon05 May 05 '11 at 04:57