Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What integer does DateTime.CompareTo actually return?

I have been looking for an answer for some time now, but nowhere could I actually find it.

I was especially looking at this page. There it says that the CompareTo method returns an integer indicating if it is earlier, the same, or later. I understand the use of it and I understand that for earlier times the integer is negative, for the same it is 0 etc.

But what is this integer? Does it return the difference in seconds, milliseconds, ticks, or maybe nothing at all? I hope you can help me with this and if anyone can find another post with this question, please tell me. I am honestly quite surprised that I couldn't find a question on this topic straight away...

like image 491
philkark Avatar asked Dec 02 '12 11:12

philkark


People also ask

What does CompareTo return in C#?

CompareTo() method in C# is used to compare this instance to a specified object or another Int16 instance and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object or the other Int16 instance.

What does date CompareTo do in Java?

Date compareTo() method in Java with examples It returns the value 0 if the argument Date is equal to this Date. It returns a value less than 0 if this Date is before the Date argument. It returns a value greater than 0 if this Date is after the Date argument.

How do I check if one date is greater than another in C#?

Compare() Method in C# This method is used to compare two instances of DateTime and returns an integer that indicates whether the first instance is earlier than, the same as, or later than the second instance. Syntax: public static int Compare (DateTime t1, DateTime t2);


3 Answers

The documentation is actually in the IComparable interface page (that DateTime implements): http://msdn.microsoft.com/en-us/library/system.icomparable.aspx

The implementation of the CompareTo(Object) method must return an Int32 that has one of three values, as shown in the following table.

Less than zero: The current instance precedes the object specified by the CompareTo method in the sort order.

Zero: This current instance occurs in the same position in the sort order as the object specified by the CompareTo method.

Greater than zero: This current instance follows the object specified by the CompareTo method in the sort order.

like image 169
kabaros Avatar answered Sep 28 '22 11:09

kabaros


There is nothing specified, according to MSDN:

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

If you want to compare days between 2 DateTimes you should be looking for something like this:

if ((expiryDate - DateTime.Now).Days < 30)
like image 29
RvdK Avatar answered Sep 28 '22 12:09

RvdK


It is an implementation detail that you should never need to know and can change at any time. The only 3 categories are:

  • negative
  • zero
  • positive

If you find yourself using anything more than that, then something is wrong.

like image 27
Marc Gravell Avatar answered Sep 28 '22 12:09

Marc Gravell