【问题标题】:Check if a DayOfWeek is within two specified DayOfWeek's检查 DayOfWeek 是否在两个指定的 DayOfWeek 内
【发布时间】:2015-07-22 17:52:51
【问题描述】:

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

【问题讨论】:

    标签: c# extension-methods dayofweek


    【解决方案1】:

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

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

    正如@Tyrsius 指出的那样,这只是因为monday &lt; 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))) {
    

    【讨论】:

    • 这实际上不起作用,因为数字不会换行。例如,它会在“星期六和星期二之间的星期一”失败
    • 我不确定您的第二个 if 语句是否正常工作。我使用今天变量Wednesday,开始日期(星期一变量)为Monday,结束日期(星期五变量)为Sunday 来运行它。不幸的是没有运气。 - 我会更新问题以提供更多信息。
    • .NET 的一周从星期日开始。
    • 啊!奇迹般有效。谢谢。
    【解决方案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);
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-11
      • 2016-09-16
      • 2015-10-15
      • 1970-01-01
      相关资源
      最近更新 更多