Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison between time of 2 DateTime c#

Tags:

c#

datetime

What is the best way to compare the time of 2 DateTime objects?

For example

  • DateTime1 = 2012-07-30 01:00
  • DateTime2 = 2012-08-01 02:00

I just need to compare the time NOT the date.

Thanks

like image 240
Maxime Avatar asked Dec 04 '22 02:12

Maxime


2 Answers

if (DateTime1.TimeOfDay > DateTime2.TimeOfDay)
{
    MessageBox.Show("DateTime1 is later");
}
like image 187
CSharpDev Avatar answered Dec 22 '22 18:12

CSharpDev


You can use DateTime.TimeOfDay to get just the time part to compare. This is essentially the same as if you did d - d.Date.

like image 26
Joey Avatar answered Dec 22 '22 18:12

Joey