gpt4 book ai didi

c# - 为什么 datetime 不能比较?

转载 作者:IT王子 更新时间:2023-10-29 03:59:03 25 4
gpt4 key购买 nike

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

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

为什么失败并显示以下信息:

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

它们不一样吗?

更新:

如果你只关心第二个,请使用这个:

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

最佳答案

您是否已验证滴答数/毫秒数是否相等?

如果您连续两次执行 DateTime.Now(),它们将显示为相同的数字,精确到分钟,甚至可能精确到 ,但它们通常会因滴答而异。如果您只想检查分钟的相等性,请仅将每个 DateTime 比较到那个程度。有关四舍五入日期时间的信息,请参阅 here


A note about resolution :

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.

关于c# - 为什么 datetime 不能比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2732206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com