检查 DayOfWeek 是否在两个指定的 DayOfWeek 内

Check if a DayOfWeek is within two specified DayOfWeek's

我有一个 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() 将 return 一个 DateTime 的列表,代表今天的日程安排。

您可以像比较数字一样比较枚举:

if (today >= monday && today <= friday) {

正如@Tyrsius 指出的那样,这只有效,因为 monday < friday。因此,从技术上讲,您需要先检查一下:

if ((monday <= friday && (today >= monday && today <= friday)) ||
    (monday > friday  && (today <= monday && today >= friday))) {

请注意 .NET 周从星期日开始:DayOfWeek.Sunday 为 0。

如果您希望一周从星期一开始,则必须执行一些算术运算。

var lowLimit = ((int)monday + 6) % 7;
var highLimit = ((int)friday + 6) % 7;
var valueToCheck = ((int)today + 6) % 7;

if ((lowLimit <= highLimit && (valueToCheck >= lowLimit && valueToCheck <= highLimit)) ||
    (lowLimit > highLimit  && (valueToCheck <= lowLimit && valueToCheck >= highLimit))) {

如前所述,您可以在逻辑比较中使用枚举,但存在值不回绕的问题。端点的顺序很重要。例如 "Is Monday between Saturday and Tuesday" 应该 return true 而 "Is Monday between Tuesday and Saturday" 应该 return false.

public static bool IsBetween(this DayOfWeek weekday, DayOfWeek inclusiveStart, DayOfWeek inclusiveEnd)
{
    if (inclusiveStart <= inclusiveEnd)
    {
        return (weekday >= inclusiveStart) && (weekday <= inclusiveEnd);
    }
    return (weekday >= inclusiveStart) || (weekday <= inclusiveEnd);
}

这应该有效。还有其他方法可以做到这一点,但这是一种方法。