Math.Min与放大器的等效; Math.Max​​的日期?放大器、日期、Math、Min

2023-09-03 10:37:05 作者:别搅

什么是获得两个日期之间的最小(或最大)值的最快和最简单的方法是什么?是否有一个相当于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.)

 
精彩推荐
图片推荐