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

后端 未结 9 1916
渐次进展
渐次进展 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:41

    Now that we have LINQ, you can create an array with your two values (DateTimes, TimeSpans, whatever) and then use the .Max() extension method.

    var values = new[] { Date1, Date2 }; 
    var max = values.Max(); 
    

    It reads nice, it's as efficient as Max can be, and it's reusable for more than 2 values of comparison.

    The whole problem below worrying about .Kind is a big deal... but I avoid that by never working in local times, ever. If I have something important regarding times, I always work in UTC, even if it means more work to get there.

提交回复
热议问题