@Brad's point that any day of the week falls between any two days of the week is valid. However, we are assuming the two days of the week in question are ordered.
That is, when we say, "is October 30, 2010 (a Saturday) between Friday and Sunday?", we are really asking, "is October 30, 2010 either a Friday, a Saturday, or a Sunday?".
This observation allows us to break down the problem into two components and solve the full problem easily:
1) Determine if a particular day of the week is one of a particular set of days of the week (this is trivial).
2) Determine the set of days of the week that take you from one day to another. That is, we want a function that returns "Friday, Saturday, Sunday" when given "Friday" and "Sunday", and that returns "Monday, Tuesday, Wednesday, Thursday, Friday" when given "Monday" and "Friday". This is the tricky part of the problem.
To solve the second problem, we basically walk from the first day to the second day, returning all days in-between. To do this correctly, we have to account for the fact that the second day may be less than the first day (in the representational sense of Sunday = 0 being less than Friday = 5). So, we perform the "walk" in an integer space, and add 7 to the second day if it is less than the first day. We convert to days-of-the-week space (which is the integers modulo 7) on the "way out".
Below is the code and a series of tests that solve this. The "GetDaysBetweenInclusive" method solves problem #2, and "IsDayOfWeekBetween" adds the solution to problem #1 and solves the OP's problem.
Enjoy.
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace DayOfWeekUtilities
{
public static class DayOfWeekHelpers
{
/// <summary>
/// returns all days of the week, inclusive, from day1 to day2
/// </summary>
public static IEnumerable<DayOfWeek> GetDaysBetweenInclusive(DayOfWeek day1,
DayOfWeek day2)
{
var final = (int)day2;
if(day2 < day1)
{
final += 7;
}
var curr = (int)day1;
do
{
yield return (DayOfWeek) (curr%7);
curr++;
} while (curr <= final);
}
/// <summary>
/// returns true if the provided date falls on a day of the
/// week between day1 and day2, inclusive
/// </summary>
public static bool IsDayOfWeekBetween(this DateTime date,
DayOfWeek day1,
DayOfWeek day2)
{
return GetDaysBetweenInclusive(day1, day2).Contains(date.DayOfWeek);
}
}
[TestFixture]
public class Tests
{
[Test]
public void Test()
{
Assert.IsTrue(new DateTime(2010, 10, 22).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Sunday));
Assert.IsTrue(new DateTime(2010, 10, 23).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Sunday));
Assert.IsTrue(new DateTime(2010, 10, 24).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Sunday));
Assert.IsFalse(new DateTime(2010, 10, 25).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Sunday));
Assert.IsFalse(new DateTime(2010, 10, 26).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Sunday));
Assert.IsFalse(new DateTime(2010, 10, 27).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Sunday));
Assert.IsFalse(new DateTime(2010, 10, 28).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Sunday));
Assert.IsTrue(new DateTime(2010, 10, 29).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Sunday));
Assert.IsTrue(new DateTime(2010, 10, 22).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Friday));
Assert.IsFalse(new DateTime(2010, 10, 23).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Friday));
Assert.IsFalse(new DateTime(2010, 10, 24).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Friday));
Assert.IsFalse(new DateTime(2010, 10, 25).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Friday));
Assert.IsFalse(new DateTime(2010, 10, 26).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Friday));
Assert.IsFalse(new DateTime(2010, 10, 27).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Friday));
Assert.IsFalse(new DateTime(2010, 10, 28).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Friday));
Assert.IsTrue(new DateTime(2010, 10, 29).IsDayOfWeekBetween(DayOfWeek.Friday, DayOfWeek.Friday));
Assert.IsTrue(new DateTime(2010, 10, 22).IsDayOfWeekBetween(DayOfWeek.Monday, DayOfWeek.Friday));
Assert.IsFalse(new DateTime(2010, 10, 23).IsDayOfWeekBetween(DayOfWeek.Monday, DayOfWeek.Friday));
Assert.IsFalse(new DateTime(2010, 10, 24).IsDayOfWeekBetween(DayOfWeek.Monday, DayOfWeek.Friday));
Assert.IsTrue(new DateTime(2010, 10, 25).IsDayOfWeekBetween(DayOfWeek.Monday, DayOfWeek.Friday));
Assert.IsTrue(new DateTime(2010, 10, 26).IsDayOfWeekBetween(DayOfWeek.Monday, DayOfWeek.Friday));
Assert.IsTrue(new DateTime(2010, 10, 27).IsDayOfWeekBetween(DayOfWeek.Monday, DayOfWeek.Friday));
Assert.IsTrue(new DateTime(2010, 10, 28).IsDayOfWeekBetween(DayOfWeek.Monday, DayOfWeek.Friday));
Assert.IsTrue(new DateTime(2010, 10, 29).IsDayOfWeekBetween(DayOfWeek.Monday, DayOfWeek.Friday));
Assert.IsTrue(new DateTime(2010, 10, 22).IsDayOfWeekBetween(DayOfWeek.Thursday, DayOfWeek.Tuesday));
Assert.IsTrue(new DateTime(2010, 10, 23).IsDayOfWeekBetween(DayOfWeek.Thursday, DayOfWeek.Tuesday));
Assert.IsTrue(new DateTime(2010, 10, 24).IsDayOfWeekBetween(DayOfWeek.Thursday, DayOfWeek.Tuesday));
Assert.IsTrue(new DateTime(2010, 10, 25).IsDayOfWeekBetween(DayOfWeek.Thursday, DayOfWeek.Tuesday));
Assert.IsTrue(new DateTime(2010, 10, 26).IsDayOfWeekBetween(DayOfWeek.Thursday, DayOfWeek.Tuesday));
Assert.IsFalse(new DateTime(2010, 10, 27).IsDayOfWeekBetween(DayOfWeek.Thursday, DayOfWeek.Tuesday));
Assert.IsTrue(new DateTime(2010, 10, 28).IsDayOfWeekBetween(DayOfWeek.Thursday, DayOfWeek.Tuesday));
Assert.IsTrue(new DateTime(2010, 10, 29).IsDayOfWeekBetween(DayOfWeek.Thursday, DayOfWeek.Tuesday));
}
}
}