我正在尝试创建一个程序,该程序将跳过当天没有商店开门的日子。
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;
}
```发布于 2019-07-12 10:01:33
出现这个问题是因为您没有跟踪下一次循环迭代中“跳过”的天数。有多种方法可以解决这个问题。
一种可能性是让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;
}
...
}https://stackoverflow.com/questions/56999160
复制相似问题