比较两个日期时间时忽略毫秒
我正在比较两个文件的 LastWriteTime,但是它总是失败,因为我从网上下载的文件总是将毫秒设置为 0,而我的原始文件具有实际值。有没有一种简单的方法可以在比较时忽略毫秒?
这是我的功能:
//compare file's dates
public bool CompareByModifiedDate(string strOrigFile, string strDownloadedFile)
{
DateTime dtOrig = File.GetLastWriteTime(strOrigFile);
DateTime dtNew = File.GetLastWriteTime(strDownloadedFile);
if (dtOrig == dtNew)
return true;
else
return false;
}
I am comparing the LastWriteTime of two files, however it is always failing because the file I downloaded off the net always has milliseconds set at 0, and my original file has an actual value. Is there a simple way to ignore the milliseconds when comparing?
Here's my function:
//compare file's dates
public bool CompareByModifiedDate(string strOrigFile, string strDownloadedFile)
{
DateTime dtOrig = File.GetLastWriteTime(strOrigFile);
DateTime dtNew = File.GetLastWriteTime(strDownloadedFile);
if (dtOrig == dtNew)
return true;
else
return false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我建议您使用扩展方法:
那么它只是:
I recommend you use an extension method:
then its just:
如果
dt
具有非零微秒(毫秒的分数),则应小心。仅将毫秒设置为零是不够的。要将毫秒及以下设置为零(并获得成功的比较),代码为:
Care should be taken, if
dt
has non-zero microseconds (fractions of millis). Setting only milliseconds to zero is not enough.To set millis and below to zero (and get a succesfull comparison), the code would be:
创建一个新的 DateTime 值,并将毫秒部分设置为 0:
Create a new DateTime value with the milliseconds component set to 0:
您可以将它们相减,以获得
TimeSpan
。然后使用TimeSpan.totalSeconds()
You can subtract them, to get a
TimeSpan
.Then use
TimeSpan.totalSeconds()
对于单个截断来说,这有点过分了,但是如果您有多个不同类型的截断,则可以使用下面的通用扩展方法来执行此操作:
更通用 使用扩展方法:
This is overkill for a single Truncate, but if you have several and of various types you could do this using the generalized Extension Method below:
More general Use Extension method:
这是执行此操作的最简单方法。您可以根据需要控制
精度
。并且用法是不言自明的
Here is the simplest way of doing this. You can control
precision
as you want.and usage is pretty self-explanatory
一种方法是创建新日期,将年、月、日、小时、分钟、秒输入到构造函数中。或者,您可以简单地分别比较每个值。
One way would be to create new dates, inputting the year, month, day, hour, minute, second into the constructor. Alternatively, you could simply compare each value separately.
不要通过创建新的 DateTime 来修剪不相关的 DateTime 部分,而是仅比较相关的部分:
我采用了 Dean Chalk 的回答作为性能比较的基础,结果是:
CompareWith
比TrimMilliseconds
快一点在相同日期的情况下CompareWith
比日期不相等我的性能测试(在控制台项目中运行)
instead of trimming unrelevant DateTime parts via creating new
DateTime
s, compare only relevant parts:I took answer by Dean Chalk as base for performance comparison, and results are:
CompareWith
is a bit faster thanTrimMilliseconds
in case of equal datesCompareWith
is a faster than dates are not equalmy perf test (run in Console project)
Ether 将其他日期时间中的毫秒设置为零,或者从另一个日期中减去一个日期,然后仅检查结果时间跨度的
TotalMinutes
属性。Ether set the milliseconds in your other datetime to zero, or subtract one date from the other and just check the
TotalMinutes
property of the resulting time span.您可以创建一个扩展方法,将 DateTime 对象的毫秒设置为零
然后在您的函数中
You could create an extension method that would set the milliseconds to zero for a DateTime object
Then in your function
只需将日期时间格式与您想要的格式一起使用,然后将其再次转换为日期时间,如下所示,
Simply you can use datetime format with the format you want, and convert it again to datetime as below,
转换可排序字符串并进行比较。简单且运行良好。
cast sortable strings and compare. simple and run well.
截断时间最直接的方法是对其进行格式化并解析所需的单位:
重写 DOK 的方法
The most straightforward way to truncate time is to format it and parse on the units that you want:
DOK's method re-written
不知道为什么几乎所有程序员都需要额外的行来从带有 bool 表达式的函数返回 bool 值。
相反,
您始终可以只使用
if 表达式为 true 则返回 true else false。
Don't know why almost all programmers needs extra lines to return a bool value from a function with a bool expression.
instead
you can always just use
if the expression is true it returns true else false.