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...
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.
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.
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);
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.
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)
It is an implementation detail that you should never need to know and can change at any time. The only 3 categories are:
If you find yourself using anything more than that, then something is wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With