What integer does DateTime.CompareTo actually return?
Asked Answered
B

6

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

Backandforth answered 2/12, 2012 at 11:48 Comment(0)
M
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.

Millais answered 2/12, 2012 at 11:53 Comment(0)
R
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)
Rusch answered 2/12, 2012 at 11:52 Comment(0)
A
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.

Amery answered 2/12, 2012 at 11:50 Comment(7)
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?Backandforth
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.Lorenzoloresz
@phil13131 to find the difference between two times: subtract them to get a timespanAmery
@Lorenzoloresz 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.Orebro
@Orebro I understand that. My only claim is that the question is still interesting, despite being an implementation detail.Lorenzoloresz
Thank you for the answer. I just thought that it would make more sense if CompareTo actually returned something "more useful".Backandforth
@phil13131 That would make it far less efficient as an implementation of CompareTo, which is used mainly for sorting.Lorenzoloresz
L
0

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

Lorenzoloresz answered 2/12, 2012 at 11:51 Comment(3)
Actually there's no guarantee that -1 or 1 will be returned.Chrissa
@PLB What values would you feed it to get a number other than those?Lorenzoloresz
any negative or positive number, respectively.Chrissa
O
0

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

Orebro answered 2/12, 2012 at 11:51 Comment(1)
(Pre-edit) The page you link to expressly says -ve, zero, +ve; not -1,0,1Amery
R
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;

}

Reprisal answered 4/4, 2018 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.