Is there between DateTime in C# ? I know I can do simple check with if (a > date1 && a < date2)
but I was trying to find Between
meth
Why restrict to just dates, use the IComparable interface.
public static bool InclusiveBetween (this IComparable a, IComparable b, IComparable c)
{
return a.CompareTo(b) >= 0 && a.CompareTo(c) <= 0;
}
public static bool ExclusiveBetween (this IComparable a, IComparable b, IComparable c)
{
return a.CompareTo(b) > 0 && a.CompareTo(c) < 0;
}
public static bool SqlBetween (this IComparable a, IComparable b, IComparable c)
{
return a.InclusiveBetween(b, c);
}
I use something similar to Richard Schneider's (universal between) and Gary Pendlebury's answer (simpler configurable boundary inclusion)
public static bool Between(this IComparable value, IComparable lowerBoundary, IComparable upperBoundary,
bool includeLowerBoundary=true, bool includeUpperBoundary=true)
{
var lower = value.CompareTo(lowerBoundary);
var upper = value.CompareTo(upperBoundary);
return (lower > 0 || (includeLowerBoundary && lower == 0)) &&
(upper < 0 || (includeUpperBoundary && upper == 0));
}
There is not, but if you obey number line formatting per Code Complete, the raw code looks simpler:
if((lowDate < a) && (a < highDate))
You can add an extension method :
public static Boolean Between(this DateTime input, DateTime minDate, DateTime maxDate)
{
// SQL takes limit in !
return input >= minDate && input <= maxDate;
}
There is not a Between
function but should be easy enough to add one
public static bool Between(DateTime input, DateTime date1, DateTime date2)
{
return (input > date1 && input < date2);
}
No, there is not.