1

I have a range of days that I need to find out if a weekend is in this range. If so I add 2 days to the final range (Monday to Sunday, changes it to Monday to Wednesday. (based on the user selection number of days E.g 5)

I have made a way to parse the days (not sure if it's the best practice) & not sure how to continue. So far:

////TODO Refactor to new class
DateTime startingDate = DateTime.Parse("07/25/2018");//TODO Insert variable from poted form 
DateTime endingDate = DateTime.Parse("07/08/2018");//TODO Insert variable from posted form

 var days=  Enumerable.Range(0, 1 + endingDate.Subtract(startingDate).Days)
                .Select(offset => startingDate.AddDays(offset))
                .ToList();

In my opinion, I need to do it like the following pseudo-code:

if day.contains dayOfWeek.Saturday ||Or dayofWeek.Friday {Add.Days(2)}

But I can't find out how to .

Draken
  • 3,134
  • 13
  • 34
  • 54
Assaf Our
  • 611
  • 3
  • 9
  • 25
  • 2
    seems to be underspecified... two days per weekend, or interval limited to contain one weekend max, what if border case (begin or end on saturday), ... I would be more comfortable attempting an answer if you could perhaps add some sample input/expected output. – Cee McSharpface Jul 26 '18 at 13:45
  • 2
    If it ends at saturday you still want to add two days? What is it ends 2 weeks later, you want to add 4 days? I guess you don't have a clear specification of what you need and the two days are just your interpretation. – Tim Schmelter Jul 26 '18 at 13:46
  • thanks ,ill try to be clear , if in this range of date e.g. the user ask to supply in 5 days I need to count only working days (Sun, Sat , are out ) so I need to provide a calendar date with this 2 days out. Hope its clear – Assaf Our Jul 26 '18 at 13:52
  • The user suplies the start date and the number of working days? – Magnetron Jul 26 '18 at 13:54
  • public holidays matter? – BugFinder Jul 26 '18 at 13:55
  • no. a) count the number of saturdays and sundays that occur in the interval, and shift the end date back accordingly, or b) compute the number of days between start and end date, and subtract the number of saturdays and sundays, so you get a number of working days as the result? give examples! – Cee McSharpface Jul 26 '18 at 13:55
  • start date is calculated & the user write number of working days until supply – Assaf Our Jul 26 '18 at 13:57
  • public holidays matter =no – Assaf Our Jul 26 '18 at 13:57
  • ok in this case it is likely just a duplicate of [Calculate the number of business days between two dates?](https://stackoverflow.com/questions/1617049/calculate-the-number-of-business-days-between-two-dates) – Cee McSharpface Jul 26 '18 at 13:58
  • I have edit my Q. – Assaf Our Jul 26 '18 at 14:03

3 Answers3

2

User suplying the end date and the number of working days, this will produce a list with the desired amount

DateTime endingDate = DateTime.ParseExact("07/25/2018","MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);//TODO Insert variable from poted form 
int workingDays = 5;
List<DateTime> dates = new List<DateTime>();
DateTime startingDate = endingDate;
while(dates.Count<workingDays)
{
    if(startingDate.DayOfWeek != DayOfWeek.Sunday && startingDate.DayOfWeek != DayOfWeek.Saturday)
    {
        dates.Insert(0, startingDate);
    }
    startingDate = startingDate.AddDays(-1);
}

Output will be

2018-07-19 
2018-07-20 
2018-07-23 
2018-07-24 
2018-07-25 

For 20 working days:

01   2018-06-28   Thursday
02   2018-06-29   Friday
03   2018-07-02   Monday
04   2018-07-03   Tuesday
05   2018-07-04   Wednesday
06   2018-07-05   Thursday
07   2018-07-06   Friday
08   2018-07-09   Monday
09   2018-07-10   Tuesday
10   2018-07-11   Wednesday
11   2018-07-12   Thursday
12   2018-07-13   Friday
13   2018-07-16   Monday
14   2018-07-17   Tuesday
15   2018-07-18   Wednesday
16   2018-07-19   Thursday
17   2018-07-20   Friday
18   2018-07-23   Monday
19   2018-07-24   Tuesday
20   2018-07-25   Wednesday
Magnetron
  • 7,495
  • 1
  • 25
  • 41
1

The first you count all the weekend days. And then, add result into the starting date. Here my solution. Hope to help, my friend!

static IEnumerable<DateTime> GetDaysBetween(DateTime start, DateTime end)
        {
            for (DateTime i = start; i < end; i = i.AddDays(1))
            {
                yield return i;
            }
        }

DateTime startingDate = DateTime.Parse("07/25/2018");//TODO Insert variable from poted form 

var numOfWeekends = GetDaysBetween(startingDate, startingDate.AddDays(7))
            .Where(d => d.DayOfWeek == DayOfWeek.Saturday || d.DayOfWeek == DayOfWeek.Sunday).Count();

startingDate = startingDate.AddDays(numOfWeekends);
            Console.WriteLine(startingDate);
Tomato32
  • 2,145
  • 1
  • 10
  • 10
  • @AssafOur I don't know your requirements, but be careful because for larger intervals, you could potencialy be inserting weekend days in the last step. For instance, if the interval contains 7 weekend days, then in the last step you are introducing 2 weekend days – Magnetron Jul 26 '18 at 14:21
1

I didn't really understand the addintion part, but this code will give you a List without any Sundays or Saturdays :

DateTime startingDate = DateTime.Parse("07/25/2018");//TODO Insert variable from poted form 
DateTime endingDate = DateTime.Parse("07/08/2018");//TODO Insert variable from posted form

List<DateTime> dates = new List<DateTime>();

for (DateTime dt = startingDate; dt <= endingDate; dt = dt.AddDays(1))
    if (dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday)
        dates.Add(dt);

You can also add a counter if you want :

DateTime startingDate = DateTime.Parse("07/25/2018");//TODO Insert variable from poted form 
DateTime endingDate = DateTime.Parse("07/08/2018");//TODO Insert variable from posted form

List<DateTime> dates = new List<DateTime>();
int couter = 0;

for (DateTime dt = startingDate; dt <= endingDate; dt = dt.AddDays(1))
{
    if (dt.DayOfWeek != DayOfWeek.Saturday && dt.DayOfWeek != DayOfWeek.Sunday)
        dates.Add(dt);

    else
        counter++;
}

Or anything else...

Guibi
  • 115
  • 1
  • 7