У меня DayOfWeek
, и мне нужно проверить, не находится ли этот день между двумя другими DayOfWeek
переменными.
Например:
DayOfWeek monday = DayOfWeek.Monday;
DayOfWeek friday= DayOfWeek.Friday;
DayOfWeek today = DateTime.Today.DayOfWeek;
if (today is between monday and friday)
{
...
}
Примечание. В эти дни будет включительно. В этом случае, если день понедельник, вторник, среда, четверг и пятница, то он действителен.
Единственное, что я могу придумать, - это сделать массивный оператор if в другом методе, возможно, в методе расширения, но это не очень элегантно.
Изменить
Вот пример моих требований:
public enum RecurringModes
{
Minutes,
Hours
}
public RecurringModes RecurringMode { get; set; }
public int RecurringValue { get; set; }
...
public IEnumerable<DateTime> AllDueDatesToday()
{
//Get the current date (starting at 00:00)
DateTime current = DateTime.Today;
//Get today and tomorrow's day of week.
DayOfWeek today = current.DayOfWeek;
DayOfWeek tomorrow = current.AddDays(1).DayOfWeek;
//If it isn't in the date range, then return nothing for today.
if (!IsInDateRange(today, StartingOn, EndingOn))
yield break;
while (current.DayOfWeek != tomorrow)
{
//Check the selected recurring mode
switch (RecurringMode)
{
//If it's minutes, then add the desired minutes
case RecurringModes.Minutes:
current = current.AddMinutes(RecurringValue);
break;
//If it's hours, then add the desired hours.
case RecurringModes.Hours:
current = current.AddHours(RecurringValue);
break;
}
//Add the calculated date to the collection.
yield return current;
}
}
public bool IsInDateRange(DayOfWeek day, DayOfWeek start, DayOfWeek end)
{
//if they are all the same date
if (start == end && start == day)
return true;
//This if statement is where the problem lies.
if ((start <= end && (day >= start && day <= end)) ||
(start > end && (day <= start && day >= end)))
return true;
else return false;
}
Фактически, метод AllDueDatesToday()
вернет список из DateTime
, который представляет расписание на сегодня.