开发者

DateTime Compare in c#

I want to compare two dateTime.

Ex:

  date1 = 13/01/2004 12:20:00
  date2 = 13/01/2004 12:35:0开发者_StackOverflow社区0
  result = Compare(date2-date1);
  O/P : 15 Minutes


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


Try this:

TimeSpan diff = date2.Subtract(date1);


How about

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


You can use

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

To get the total difference in minutes.


How about:

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


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


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.


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


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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜