日期时间比较精密 [英] DateTime Comparison Precision

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

问题描述

我做的日期时间的比较,但我不想做第二个时比较,毫秒和扁虱水平。什么是最优雅的方式?

I'm doing DateTime comparison but I don't want to do comparison at second, millisecond and ticks level. What's the most elegant way?

如果我只是比较日期时间,然后他们很少等于因蜱差异。

If I simply compare the DateTime, then they are seldom equal due to ticks differences.

推荐答案

怎么样使用的时间跨度。

What about using a timespan.

if (Math.Truncate((A - B).TotalMinutes) == 0)
{
    //There is less than one minute between them
}

也许不是最优雅的方式,但它允许它在一秒分开,但有不同的天/小时/分部位,如打算在午夜的情况。

Probably not the most elegant way, but it allows for cases which are one second apart and yet have different days/hours/minutes parts such as going over midnight.

修改:它发生,我认为截断是不必要的...

Edit: it occured to me that the truncate is unecessary...

if (Math.Abs((A - B).TotalMinutes) < 1)
{
    //There is less than one minute between them
}

我个人认为这是更优雅...

Personally I think this is more elegant...

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

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