By default C# compares DateTime objects to the 100ns tick. However, my database returns DateTime values to the nearest millisecond. What\'s the best way to compare two DateT
I’ve created extension methods IsSimilar
public static bool IsSimilar(this DateTime? lhs, DateTime? rhs, TimeSpan tolerance)
{
if (!lhs.HasValue && !lhs.HasValue) return true;//both are null
if (!lhs.HasValue || !lhs.HasValue) return false;//one of 2 is null
return IsSimilar(lhs.Value, rhs.Value, tolerance);
}
public static bool IsSimilar(this DateTime lhs, DateTime rhs, TimeSpan tolerance)
{
return (lhs - rhs).Duration() <= tolerance;
}