Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime Compare in c#

Tags:

c#

datetime

I want to compare two dateTime.

Ex:

  date1 = 13/01/2004 12:20:00
  date2 = 13/01/2004 12:35:00
  result = Compare(date2-date1);
  O/P : 15 Minutes
like image 264
Ravi Avatar asked May 19 '10 15:05

Ravi


2 Answers

To compare, you can simply use the < operator: date1 < date2.

If you want to compare with a given resolution, try date1.TotalMinutes == date2.TotalMinutes (this compared for the same minute).

If you want to know if the difference is within a certain time span, use this:

System.TimeSpan dt = date2.Subtract(date1);
if (dt.TotalMinutes < 15) //...
like image 194
mafu Avatar answered Oct 04 '22 08:10

mafu


Try this:

TimeSpan diff = date2.Subtract(date1);
like image 34
D. Tony Avatar answered Oct 04 '22 06:10

D. Tony