What integer does DateTime.CompareTo actually return?

philkark picture philkark · Dec 2, 2012 · Viewed 23.4k times · Source

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

Answer

kabaros picture kabaros · Dec 2, 2012

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.