0

Im trying to do this:

var startDate = DateTime.Now

var durationDays = 2

var expectedEndDate = startDate.AddDays(durationDays)

But if there are weekend days between these days, discount them as days. So if the expected end date falls on a saturday then end date needs to be on the following monday.

user1809104
  • 707
  • 2
  • 8
  • 26

4 Answers4

2

Depending on the number of days you want to add as a maximum, you could just check for the day in a while loop and add another day via AddDays as long as the current day of the week of expectedEndDate falls on a weekend. Maybe something like this:

DateTime startDate = DateTime.Now
int durationDays = 2
DateTime expectedEndDate = startDate.AddDays(durationDays)
while( expectedEndDate.DayOfWeek == DayOfWeek.Saturday ||
       expectedEndDate.DayOfWeek == DayOfWeek.Sunday )
{
    expectedEndDate.AddDays( 1 );
}

If you need a more flexible solution for larger date differences covering multiple weekends, the linked solution from another post is for you (I was trying to keep it simple :-)).

Gorgsenegger
  • 7,356
  • 4
  • 51
  • 89
  • 1
    This wont work. If the end date is a saturday then the end date will be sunday (saturday + 1)... which is wrong. – user1809104 Aug 22 '14 at 10:38
  • 1
    @user1809104 It will work - the next time the condition for the loop is verified, it will add another day. – Gorgsenegger Aug 22 '14 at 10:40
  • Still doesnt seem like itd work. If today is friday, and the duration is one day it will count 1 day, so the end date will be a saturday... – user1809104 Aug 22 '14 at 10:59
  • 1
    If you use your code and then add mine at the end, you will not end up on a Saturday but Monday, the while loop would run twice: If today was Friday and you would add 1 day, the first check of the condition would return true for Saturday, causing the loop to be executed, adding one (1) day. The next check would then again return true, this time for Sunday, therefore executing and adding one (1) day again. – Gorgsenegger Aug 22 '14 at 11:04
  • @user1809104 Why not *try* it and *see* if it works or not? – Nahuel Ianni Aug 22 '14 at 11:23
  • I have. Sorry i may have not provided enough info. The code you provided works as you would expect. But if I change the duration days to 9 for instance. I need the 'expectedEnddate' to be 9 working days in the future. So today it the 22nd. 9 working days in the future would be 4 september. That code returns 1 september. – user1809104 Aug 22 '14 at 11:27
  • No problem, but you should have been more specific in your question. The linked solution (comment under your question) should be what you need, but that's overkill if you want a simple solution only covering a number of days that does not span two weekends. – Gorgsenegger Aug 22 '14 at 11:34
  • If youd like to take a crack at a simpler solution, go ahead:P – user1809104 Aug 22 '14 at 11:42
0

You could also use this solution, which should be more efficient, if u are checking in a larger Timespan: Calculate the number of business days between two dates?

Community
  • 1
  • 1
0

Try this:

        var startDate = DateTime.Now;
        var durationDays = 2;
        var expectedEndDate = startDate.AddDays(durationDays);

        if (expectedEndDate.DayOfWeek == DayOfWeek.Saturday)
        {
            expectedEndDate = expectedEndDate.AddDays(2);
        }
        else
        {
            if (expectedEndDate.DayOfWeek == DayOfWeek.Sunday)
            {
                expectedEndDate = expectedEndDate.AddDays(1);
            }
        }
Ruslan Veselov
  • 337
  • 1
  • 10
  • Will only work if there is one weekend between two dates. If i put that in a loop it will add too many days because it will count sat for 2 days then add another for sunday which is three days when it should be two – user1809104 Aug 22 '14 at 11:05
  • @user1809104 About what loop are you talking? Please increase your `durationDays` in your example and provide your expectations. – Ruslan Veselov Aug 22 '14 at 11:12
0

I try to improve @Gorgsenegger 's answer :

    int iteration = 0;
        while (iteration < durationDays)
        {
            while (expectedEndDate.DayOfWeek != DayOfWeek.Saturday && expectedEndDate.DayOfWeek != DayOfWeek.Sunday)
            {
                expectedEndDate = expectedEndDate.AddDays(1);
                iteration++;
                if (iteration == durationDays) break;
            }
            if (iteration == durationDays) break;
            if (expectedEndDate.DayOfWeek == DayOfWeek.Saturday) expectedEndDate=expectedEndDate.AddDays(2);
            if (expectedEndDate.DayOfWeek == DayOfWeek.Sunday) expectedEndDate=expectedEndDate.AddDays(1);
        }
        if (expectedEndDate.DayOfWeek == DayOfWeek.Saturday)expectedEndDate= expectedEndDate.AddDays(2);
        if (expectedEndDate.DayOfWeek == DayOfWeek.Sunday) expectedEndDate=expectedEndDate.AddDays(1);

I think this snippet handles all cases

Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191