tags:

views:

98

answers:

7

Hi, 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

Thanks

+1  A: 

How about

if (date1 < date2)
{
    // date1 is before date2
}
Darin Dimitrov
A: 

Try this:

TimeSpan diff = date2.Subtract(date1);
D. Tony
+1  A: 

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) //...
mafutrct
+1  A: 

You can use

double minutes = d2.Subtract(d1).TotalMinutes;

To get the total difference in minutes.

Brandon
A: 

I don't fully understand what you're asking.

If you want your pseudo-code expressing in C# here you go...

        //date1 = 13/01/2004 12:20:00
        DateTime dateTime1 = new DateTime(2004, 01, 13, 12, 20, 0);
        //date2 = 13/01/2004 12:35:00 
        DateTime dateTime2 = new DateTime(2004, 01, 13, 12, 35, 0);

        //get the time difference - result = Compare(date2-date1); 
        TimeSpan result = dateTime2 - dateTime1;

        //output is 15
        Console.WriteLine(result.TotalMinutes);
DoctaJonez
+1  A: 

How about:

Timespan ts = date2 - date1;
Console.WriteLine("Value of Minutes = ", ts.Minutes);
Andy Johnson
A: 
DateTime date1 = DateTime.Now;
DateTime date2 = DateTime.Now;

var x = date1.CompareTo(date2);

EDIT: I see now that you wanted to get the time difference between the two dates. For that you use the TimeSpan class.

Andre