12

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

philkark
  • 2,417
  • 7
  • 38
  • 59

6 Answers6

22

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.

Community
  • 1
  • 1
kabaros
  • 5,083
  • 2
  • 22
  • 35
17

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)
RvdK
  • 19,580
  • 4
  • 64
  • 107
2

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • So that means, I cannot use it, to quickly find the difference between two times? I will have to subtract them and then look for that difference? – philkark Dec 02 '12 at 11:51
  • IMHO The fact that it's an implementation detail doesn't mean I should never need to know about it, or rather, do not **want** to know about it. The inner workings of the framework logic are interesting in themselves. – Rotem Dec 02 '12 at 11:52
  • @phil13131 to find the difference between two times: subtract them to get a timespan – Marc Gravell Dec 02 '12 at 11:54
  • @Rotem Thats not what implementation detail means. Implementation detail means it can change at any time without further warning. So you shouldn't asume it will be exact values. – Euphoric Dec 02 '12 at 11:54
  • @Euphoric I understand that. My only claim is that the question is still interesting, despite being an implementation detail. – Rotem Dec 02 '12 at 11:55
  • Thank you for the answer. I just thought that it would make more sense if CompareTo actually returned something "more useful". – philkark Dec 02 '12 at 11:55
  • @phil13131 That would make it far less efficient as an implementation of `CompareTo`, which is used mainly for sorting. – Rotem Dec 02 '12 at 11:56
0

As far as I can tell the number is always -1, 0, or 1.

Rotem
  • 21,452
  • 6
  • 62
  • 109
0

It is implementation of IComparable.CompareTo. This means it will return 0 if equal, positive integer if bigger and negative integer when smaller.

Euphoric
  • 12,645
  • 1
  • 30
  • 44
0

You can choose the specific units to compare with TimeSpan

DateTime local_time = DateTime.Now; //current time

DateTime remote_time = DateTime.Now.AddMinutes(-2); //two minutes delayed

TimeSpan time_difference = (local_time - remote_time);

if (time_difference.Minutes <= 5) //compare specific units desired

{

bool within_tollerance = true;

}

John
  • 111
  • 2
  • 2