我正在尝试创建一个程序来跳过当天没有商店开业的日子。
Ex:
Day 1 -> Friday
DAy 2 -> Saturday
Day 3 -> Sunday (no store opening)
Day 4 -> Monday
目前,我的程序将在没有开店的情况下跳过并添加1天。 所以会是这样:
Day 3 -> Sunday + 1day = Monday,
但是我的问题是..第四天也安排在星期一。
所以我的实际结果将变为:
Day 3 -> Monday
Day 4 -> Monday (which must be move on Tuesday)
我该怎么做?
这是我的代码:
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;
}
```
答案 0 :(得分:0)
之所以会出现此问题,是因为您没有在下一个循环迭代中跟踪“跳过”的日子。解决此问题的方法有多种。
一种可能性是让possibleDate
保留在for循环之外。这样一来,您就可以为每次循环添加1天。
var possibleDate = context.FirstDay.AddDays(dayOne);
for (var i = 0; i <= 3; i++)
{
possibleDate = possibleDate.AddDays(1);
if (!_storeScheduleService.IsStoreOpenForDate(storeId, possibleDate)) continue;
...
}
另一种选择是跟踪跳过的天数计数器
int skippedDays = 0;
for (var i = 0; i <= 3; i++)
{
var possibleDate = context.FirstDay.AddDays(dayOne + i + skippedDays);
possibleDate = possibleDate.AddDays(1);
if (!_storeScheduleService.IsStoreOpenForDate(storeId, possibleDate))
{
skippedDays++;
continue;
}
...
}