为什么日期时间不能比较? [英] Why datetime cannot compare?

查看:174
本文介绍了为什么日期时间不能比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#单元测试有下面的语句:

my C# unit test has the following statement:

Assert.AreEqual(logoutTime, log.First().Timestamp);



为什么失败与以下信息:

Why it is failed with following information:

Assert.AreEqual failed. Expected:<4/28/2010 2:30:37 PM>. Actual:<4/28/2010 2:30:37 PM>.



难道他们不一样?

Are they not the same?

更新:

使用这个,如果你只在乎第二:

Use this if you only care to second:

Assert.AreEqual(logoutTime.ToString(),log.First()Timestamp.ToString());

推荐答案

你有没有验证蜱数/毫秒等于?

Have you verified that the number of ticks/milliseconds are equal?

如果您 DateTime.Now( )两次回退,他们将显示为相同数量下降到分钟,甚至可能下降到的第二的,但他们往往会被蜱而异。如果你想检查平等只到分钟,每个日期时间比较只有那种程度。有关四舍五入DateTime是否信息,请参见 href=\"http://stackoverflow.com/questions/1393696/c-rounding-datetime-objects\">

If you do DateTime.Now() twice back to back, they will appear to be the same number down to the minute and probably even down to the second, but they will often vary by ticks. If you want to check equality only to the minute, compare each DateTime only to that degree. For information on rounding DateTimes, see here

一个关于解决方案注意

现在属性经常用来衡量业绩。然而,由于其分辨率低,所以不适合用作基准工具。更好的选择是使用秒表类。

The Now property is frequently used to measure performance. However, because of its low resolution, it is not suitable for use as a benchmarking tool. A better alternative is to use the Stopwatch class.

这篇关于为什么日期时间不能比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆