如何比较日期时间的时间部分时间、日期、部分

2023-09-04 00:14:11 作者:还敢心动吗

让我们说我们有

DateTime t1 = DateTime.Parse("2012/12/12 15:00:00.000");

DateTime t2 = DateTime.Parse("2012/12/12 15:03:00.000");

如何在C#中比较和说哪个时间晚于?

How to compare it in C# and say which time is "is later than"?

谢谢!

推荐答案

您可以使用的的TimeOfDay 财产和使用比较的反对。

You can use the TimeOfDay property and use the Compare against it.

TimeSpan.Compare(t1.TimeOfDay, t2.TimeOfDay)

每文档:

-1  if  t1 is shorter than t2.
0   if  t1 is equal to t2.
1   if  t1 is longer than t2.