Click here to Skip to main content
14,236,142 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to create a program that will skip the days when there is no store opening on that day.


Ex:
Day 1 -> Friday
DAy 2 -> Saturday
Day 3 -> Sunday (no store opening)
Day 4 -> Monday


Currently my program will skip and add 1 day when there is no store opening. so it will be like this:

Day 3 -> Sunday + 1day = Monday, 


but my problem is.. the Day 4 which also scheduled on Monday,.

so my actual result will become :

Day 3 -> Monday
Day 4 -> Monday (which must be move on Tuesday)


What can I do to achieve that?


What I have tried:

Here is my code:

    var dayOne = td.MinutesFromAttached.Value - 1;

                    for (var i = 0; i <= 3; i++)
                    {
                        var possibleDate = context.FirstDay.AddDays(dayOne + i);

                        if (!_storeScheduleService.IsStoreOpenForDate(storeId, possibleDate)) continue;

                        var scheduleCheck = _storeScheduleService.IsStoreOpen(context.TaskParam.Customer.StoreId.Value, possibleDate);

                        var tsDispatch = td.DispatchTime ?? new TimeSpan(9, 0, 0);

                        if (tsDispatch < scheduleCheck.Schedule.Open)
                        {
                            tsDispatch = scheduleCheck.Schedule.Open.Value;
                        }
                        else if (tsDispatch > scheduleCheck.Schedule.Close)
                        {
                            tsDispatch = scheduleCheck.Schedule.Close.Value;
                        }

                        var dateTimeSchedule = new DateTime(possibleDate.Year,
                            possibleDate.Month,
                            possibleDate.Day,
                            tsDispatch.Hours,
                            tsDispatch.Minutes,
                            tsDispatch.Seconds);

                        aTaskExec.ScheduledDispatchedDateTime = dateTimeSchedule;

                        break;
                    }
    ```
Posted
Updated 3 days ago

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

It's pretty easy. You need to use a loop, for example:

int daystoadd = 4;
DateTime initialDate = DateTime.Today;
List<DateTime> days = new List<DateTime>();
int i = 0;
while(days.Count<daystoadd)
{
	if(initialDate.AddDays(i).DayOfWeek != DayOfWeek.Sunday)
		days.Add(initialDate.AddDays(i));
	i++;
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100