检查DayOfWeek是否在指定的DayOfWeek中

时间:2015-07-22 17:52:52

标签: c# extension-methods dayofweek

我有一个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的列表,该列表代表今天的时间表。

2 个答案:

答案 0 :(得分:4)

您可以将枚举比较为数字:

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))) {

答案 1 :(得分:2)

如上所述,您可以在逻辑比较中使用枚举,但是存在值不会环绕的问题。并且终点的顺序很重要。例如“星期六和星期二之间的星期一”应该返回true而“星期二和星期六之间的星期一”应该返回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);
}

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