Equivalent of Math.Min & Math.Max for Dates?

后端 未结 9 1910
渐次进展
渐次进展 2020-12-13 05:16

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 som

9条回答
  •  攒了一身酷
    2020-12-13 05:49

    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))
    

    (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.)

提交回复
热议问题