Search code examples
c#extension-methodsdayofweek

Check if a DayOfWeek is within two specified DayOfWeek's


I have a DayOfWeek and I need to check to see if this day is between two other DayOfWeek variables.

For example:

DayOfWeek monday = DayOfWeek.Monday;
DayOfWeek friday= DayOfWeek.Friday;

DayOfWeek today = DateTime.Today.DayOfWeek;

if (today is between monday and friday)
{
   ...
}

Note: These days would be inclusive. In this case, if the day is Monday, Tuesday, Wednesday, Thursday, and Friday, then it's valid.

The only thing I can think of is doing a massive if statement in a different method, perhaps an extension method, but this isn't very elegant.

Edit

Here is an example of my requirements:

    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;
    }

Effectively, the method AllDueDatesToday() will return a list of DateTime which represents a schedule for today.


Solution

  • You can compare enums as if they were numbers:

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

    As @Tyrsius points out, this only works because monday < friday. So, technically, you need to check that first:

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

    Note that .NETs week starts at Sunday: DayOfWeek.Sunday is 0.

    If you want your week to start at Monday, you have to perform some arithmetic.

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