Ravi
Ravi

Reputation: 1273

DateTime Compare in c#

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

Upvotes: 5

Views: 26599

Answers (9)

user3517445
user3517445

Reputation: 1

In the words of Larry Wall there is more than one way to do this. If you are looking for the -1, 0, +1 result of a compare within a certain time interval try one of these variants;

   internal static int XDateCompare(DateTime date, DateTime other, int ticks)
    {
        var diff = date.Ticks - other.Ticks;

        var result = Math.Abs(diff) <= ticks ? 0
            : diff <= 0 ? -1
            : 1;

        Console.WriteLine("{0}\t{1}\t{2}\ts={3} milSec={4}", diff, other, result, ticks, date.Subtract(other).Duration().TotalMilliseconds);

        return result;
    }

    internal static int XDateCompare(DateTime date, DateTime other, double milliseconds)
    {
        double diff =
            date.Subtract(other)
            .TotalMilliseconds;

        var result = Math.Abs(diff) <= milliseconds ? 0
            : diff <= 0 ? -1
            : 1;

        Console.WriteLine("result {0} diff {1} vs ms {2}", result, diff, milliseconds);

        return result;
    }

Upvotes: 0

emekslondon
emekslondon

Reputation: 11

Now this is the best bet.

 using System;

public class Example
{
   public static void Main()
   {
      DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
      DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
      int result = DateTime.Compare(date1, date2);
      string relationship;

      if (result < 0)
         relationship = "is earlier than";
      else if (result == 0)
         relationship = "is the same time as";         
      else
         relationship = "is later than";

      Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
   }
}
// The example displays the following output: 
//    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM

Upvotes: 0

Andre
Andre

Reputation: 1107

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.

Upvotes: 0

Doctor Jones
Doctor Jones

Reputation: 21664

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);

Upvotes: 1

Andy Johnson
Andy Johnson

Reputation: 8149

How about:

Timespan ts = date2 - date1;
Console.WriteLine("Value of Minutes = ", ts.Minutes);

Upvotes: 1

Brandon
Brandon

Reputation: 69983

You can use

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

To get the total difference in minutes.

Upvotes: 3

mafu
mafu

Reputation: 32650

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) //...

Upvotes: 12

D. Tony
D. Tony

Reputation: 328

Try this:

TimeSpan diff = date2.Subtract(date1);

Upvotes: 10

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

How about

if (date1 < date2)
{
    // date1 is before date2
}

Upvotes: 4

Related Questions