Math.Min与放大器的等效; Math.Max​​的日期? [英] Equivalent of Math.Min & Math.Max for Dates?

查看:141
本文介绍了Math.Min与放大器的等效; Math.Max​​的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是获得两个日期之间的最小(或最大)值的最快和最简单的方法是什么?是否有一个相当于Math.Min(安培; Math.Max​​)吗?日期

What's the quickest and easiest way to get the Min (or Max) value between two dates? Is there an equivalent to Math.Min (& Math.Max) for dates?

我想做的事情是这样的:

I want to do something like:

 if (Math.Min(Date1, Date2) < MINIMUM_ALLOWED_DATE) {
      //not allowed to do this
 }

显然,上述Math.Min不起作用,因为他们的日期。

Obviously the above Math.Min doesn't work because they're dates.

推荐答案

有没有超载的日期时间值,但你可以得到长期价值这是什么值包含,对它们进行比较,然后创建从结果一个新的日期时间值:

There is no overload for DateTime values, but you can get the long value Ticks that is what the values contain, compare them and then create a new DateTime value from the result:

new DateTime(Math.Min(Date1.Ticks, Date2.Ticks))

(注意,日期时间结构还包含一个属性,未在新值保留这通常不是一个问题;如果你比较DateTime值不同类型的比较没有任何意义。)

(Note that the DateTime structure also contains a Kind property, that is not retained in the new value. This is normally not a problem; if you compare DateTime values of different kinds the comparison doesn't make sense anyway.)

这篇关于Math.Min与放大器的等效; Math.Max​​的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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