DateTime
объекты сопоставимы сами по себе, поэтому
if (dt1>dt2)
Console.WriteLine('dt1 is newer');
else if (dt1>dt2)
Console.WriteLine('dt2 is newer');
else // they are equal
Console.WriteLine('dt1 and dt2 are the same');
также будет работать.
Кроме того, DateTime
реализует интерфейс IComparable<DateTime>
, поэтому вы можете сделать:
int result = dt1.CompareTo(dt2);
if (result > 0)
Console.WriteLine('dt1 is newer');
else if (result < 0)
Console.WriteLine('dt2 is newer');
else // result = 0
Console.WriteLine('dt1 and dt2 are the same');
РЕДАКТИРОВАТЬ: Это игнорирует часовые пояса, местное время и т. Д ...