It may be too late, but in order to help other people who might stumble upon this, I used the extension method using IComparable as follows:
public static class BetweenExtension { public static bool IsBetween<T>(this T value, T min, T max) where T : IComparable { return (min.CompareTo(value) <= 0) && (value.CompareTo(max) <= 0); } }
Using this extension method with IComparable makes this method more general and makes it suitable for use with a wide range of data types, not just dates.
You would use it as follows:
DateTime start = new DateTime(2015,1,1); DateTime end = new DateTime(2015,12,31); DateTime now = new DateTime(2015,8,20); if(now.IsBetween(start, end)) { //Your code here }
rottenbanana Aug 20 '15 at 13:18 2015-08-20 13:18
source share