DateTimeOffset - InRange
A T extension method to determines whether the object is equal to any of the provided values.
public static void Main() { DateTimeOffset minDate = new DateTimeOffset(2007, 9, 1, 6, 45, 0, new TimeSpan(-7, 0, 0)); DateTimeOffset maxDate = new DateTimeOffset(2007, 11, 1, 6, 45, 0, new TimeSpan(-6, 0, 0)); DateTimeOffset searchDate = new DateTimeOffset(2007, 10, 1, 8, 45, 0, new TimeSpan(-5, 0, 0)); // C# Extension Method: DateTimeOffset - InRange if(searchDate.InRange(minDate, maxDate)) { Console.WriteLine("{0} is in range [{1} --- {2}]", searchDate, minDate, maxDate); } else { Console.WriteLine("{0} is not in range [{1} --- {2}]", searchDate, minDate, maxDate); } }
View Source
using System; public static partial class Extensions { /// <summary> /// A T extension method that check if the value is between inclusively the minValue and maxValue. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="minValue">The minimum value.</param> /// <param name="maxValue">The maximum value.</param> /// <returns>true if the value is between inclusively the minValue and maxValue, otherwise false.</returns> /// ### /// <typeparam name="T">Generic type parameter.</typeparam> public static bool InRange(this DateTimeOffset @this, DateTimeOffset minValue, DateTimeOffset maxValue) { return @this.CompareTo(minValue) >= 0 && @this.CompareTo(maxValue) <= 0; } }