如何获得第二个日期和时间?

时间:2014-11-10 13:00:25

标签: c#

我有两次如下:

DateTime time1=Datetimepicker1.value;
DateTime time2=file1.creationtime;

time1和time2都有日期和时间显示12-31-2014 15:00:33
但是,当我同时兼顾两次时,它会给出错误的结果:

int result=DateTime.Compare(time1,time2);

结果值为0.
怎么样?

4 个答案:

答案 0 :(得分:6)

结果是正确的。根据{{​​3}},该函数根据以下逻辑工作,假设日期名为t1t2

 __Value Type__      __Condition__ 

 Less than zero      t1 is earlier than t2. 
 Zero                t1 is the same as t2. 
 Greater than zero   t1 is later than t2. 

答案 1 :(得分:2)

如果您的代码是:

  int result = DateTime.Compare(t1,t2)

然后适用以下内容:

result = 0 => t1=t2
result < 0 => t1<t2
result > 0 => t1>t2

答案 2 :(得分:1)

DateTime.Compare方法为相同的值返回零。有关详细信息,请参阅link

enter image description here

答案 3 :(得分:-3)

DateTime time1 = Datetimepicker1.value;
DateTime time2 = file1.creationtime;
TimeSpan delta = time1 - time2;
bool areSame = Math.Abs(delta.TotalSeconds) < 1;