C# DateTime.CompareTo 实际返回什么整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13668985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What integer does DateTime.CompareTo actually return?
提问by philkark
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 CompareTomethod 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.
我特别在看这个页面。它说该CompareTo方法返回一个整数,指示它是更早、相同还是更晚。我了解它的使用,并且我了解早期的整数是负数,同样它是 0 等。
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...
但这个整数是什么?它是否返回以秒、毫秒、滴答为单位的差异,或者什么都不返回?我希望你能帮我解决这个问题,如果有人能找到关于这个问题的另一篇文章,请告诉我。老实说,我很惊讶我无法立即找到关于这个主题的问题......
采纳答案by kabaros
The documentation is actually in the IComparableinterface page (that DateTime implements): http://msdn.microsoft.com/en-us/library/system.icomparable.aspx
该文档实际上在IComparable接口页面(DateTime 实现)中: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.
CompareTo(Object) 方法的实现必须返回一个具有三个值之一的 Int32,如下表所示。
小于零:当前实例在排序顺序中位于 CompareTo 方法指定的对象之前。
零:此当前实例出现在与 CompareTo 方法指定的对象在排序顺序中相同的位置。
大于零:此当前实例在排序顺序中跟随由 CompareTo 方法指定的对象。
回答by Marc Gravell
It is an implementation detail that you should never need to know and can change at any time. The only 3 categories are:
这是一个您永远不需要知道并且可以随时更改的实现细节。仅有的3类是:
- negative
- zero
- positive
- 消极的
- 零
- 积极的
If you find yourself using anything more than that, then something is wrong.
如果你发现自己使用的东西不止这些,那就有问题了。
回答by Rotem
As far as I can tell the number is always -1, 0, or 1.
据我所知,该数字始终为 -1、0 或 1。
回答by Euphoric
It is implementation of IComparable.CompareTo. This means it will return 0 if equal, positive integer if bigger and negative integer when smaller.
它是IComparable.CompareTo 的实现。这意味着如果相等,则返回 0,如果较大则返回正整数,较小时返回负整数。
回答by RvdK
There is nothing specified, according to MSDN:
根据 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:
如果你想比较 2 个 DateTimes 之间的天数,你应该寻找这样的东西:
if ((expiryDate - DateTime.Now).Days < 30)
回答by John
You can choose the specific units to compare with TimeSpan
您可以选择要与 TimeSpan 进行比较的特定单位
DateTime local_time = DateTime.Now; //current time
DateTime local_time = DateTime.Now; //current time
DateTime remote_time = DateTime.Now.AddMinutes(-2); //two minutes delayed
DateTime remote_time = DateTime.Now.AddMinutes(-2); //two minutes delayed
TimeSpan time_difference = (local_time - remote_time);
TimeSpan time_difference = (local_time - remote_time);
if (time_difference.Minutes <= 5) //compare specific units desired
if (time_difference.Minutes <= 5) //compare specific units desired
{
{
bool within_tollerance = true;
bool within_tollerance = true;
}
}

