How to compare DateTime in C#?

Ashish Ashu picture Ashish Ashu · Jun 17, 2010 · Viewed 236.4k times · Source

I don't want user to give the back date or time.

How can I compare if the entered date and time is LESS then the current time?

If the current date and Time is 17-Jun-2010 , 12:25 PM , I want user cannot give date before 17 Jun -2010 and time before 12:25 PM.

Like my function return false if the time entered by user is 16-Jun-2010 and time 12:24 PM

Answer

MuSTaNG picture MuSTaNG · Jun 17, 2010

Microsoft has also implemented the operators '<' and '>'. So you use these to compare two dates.

if (date1 < DateTime.Now)
   Console.WriteLine("Less than the current time!");