正如其他答案所表明的那样,默认比较器对于可为空的 DateTime 对象工作得很好。您在 cmets 中询问了如何比较日期的四舍五入形式。你会想要这样的东西:
if (dt1.EqualsRounded(dt2))
以下是执行此操作所需的所有代码。它完全支持可空和不可空的日期时间。它同时实现了IComparer<T> 和IEqualityComparer<T>,因此您也可以在排序列表时使用它(例如)。像上面这样的简单情况有扩展方法,并且有重载来提供不同的舍入间隔。如果不指定一个,则默认舍入到最接近的整秒。享受吧!
public static class DateTimeExtensions
{
public static DateTime RoundToNearestInterval(this DateTime dateTime, TimeSpan interval)
{
// Adapted from http://stackoverflow.com/questions/1393696/c-rounding-datetime-objects
// do the rounding
var intervalTicks = interval.Ticks;
var ticks = (dateTime.Ticks + (intervalTicks / 2) + 1) / intervalTicks;
var totalTicks = ticks * intervalTicks;
// make sure the result is not to low
if (totalTicks < 0)
totalTicks = 0;
// make sure the result is not to high
const long maxTicks = 0x2bca2875f4373fffL; // DateTime.MaxTicks
if (totalTicks > maxTicks)
totalTicks = maxTicks;
// return the new date from the result
return new DateTime(totalTicks, dateTime.Kind);
}
public static bool EqualsRounded(this DateTime x, DateTime y)
{
return x.EqualsRounded(y, TimeSpan.FromSeconds(1));
}
public static bool EqualsRounded(this DateTime x, DateTime y, TimeSpan interval)
{
var comparer = new RoundedDateTimeComparer(interval);
return comparer.Equals(x, y);
}
public static bool EqualsRounded(this DateTime? x, DateTime? y)
{
return x.EqualsRounded(y, TimeSpan.FromSeconds(1));
}
public static bool EqualsRounded(this DateTime? x, DateTime? y, TimeSpan interval)
{
var comparer = new RoundedDateTimeComparer(interval);
return comparer.Equals(x, y);
}
public static int CompareRounded(this DateTime x, DateTime y)
{
return x.CompareRounded(y, TimeSpan.FromSeconds(1));
}
public static int CompareRounded(this DateTime x, DateTime y, TimeSpan interval)
{
var comparer = new RoundedDateTimeComparer(interval);
return comparer.Compare(x, y);
}
public static int CompareRounded(this DateTime? x, DateTime? y)
{
return x.CompareRounded(y, TimeSpan.FromSeconds(1));
}
public static int CompareRounded(this DateTime? x, DateTime? y, TimeSpan interval)
{
var comparer = new RoundedDateTimeComparer(interval);
return comparer.Compare(x, y);
}
}
public class RoundedDateTimeComparer :
IComparer<DateTime>, IComparer<DateTime?>,
IEqualityComparer<DateTime>, IEqualityComparer<DateTime?>
{
private readonly TimeSpan _interval;
public RoundedDateTimeComparer(TimeSpan interval)
{
_interval = interval;
}
public int Compare(DateTime x, DateTime y)
{
var roundedX = x.RoundToNearestInterval(_interval);
var roundedY = y.RoundToNearestInterval(_interval);
return roundedX.CompareTo(roundedY);
}
public int Compare(DateTime? x, DateTime? y)
{
return x.HasValue && y.HasValue ? Compare(x.Value, y.Value) : (y.HasValue ? 1 : (x.HasValue ? -1 : 0));
}
public bool Equals(DateTime x, DateTime y)
{
var roundedX = x.RoundToNearestInterval(_interval);
var roundedY = y.RoundToNearestInterval(_interval);
return roundedX.Equals(roundedY);
}
public bool Equals(DateTime? x, DateTime? y)
{
return x.HasValue && y.HasValue ? Equals(x.Value, y.Value) : x.Equals(y);
}
public int GetHashCode(DateTime obj)
{
var rounded = obj.RoundToNearestInterval(_interval);
return rounded.GetHashCode();
}
public int GetHashCode(DateTime? obj)
{
return obj.HasValue ? GetHashCode(obj.Value) : 0;
}
}