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
How about:
public static T Min<T>(params T[] values)
{
if (values == null) throw new ArgumentNullException("values");
var comparer = Comparer<T>.Default;
switch(values.Length) {
case 0: throw new ArgumentException();
case 1: return values[0];
case 2: return comparer.Compare(values[0],values[1]) < 0
? values[0] : values[1];
default:
T best = values[0];
for (int i = 1; i < values.Length; i++)
{
if (comparer.Compare(values[i], best) < 0)
{
best = values[i];
}
}
return best;
}
}
// overload for the common "2" case...
public static T Min<T>(T x, T y)
{
return Comparer<T>.Default.Compare(x, y) < 0 ? x : y;
}
Works with any type that supports IComparable<T>
or IComparable
.
Actually, with LINQ, another alternative is:
var min = new[] {x,y,z}.Min();
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.
public static class DateTool
{
public static DateTime Min(DateTime x, DateTime y)
{
return (x.ToUniversalTime() < y.ToUniversalTime()) ? x : y;
}
public static DateTime Max(DateTime x, DateTime y)
{
return (x.ToUniversalTime() > y.ToUniversalTime()) ? x : y;
}
}
This allows the dates to have different 'kinds' and returns the instance that was passed in (not returning a new DateTime constructed from Ticks or Milliseconds).
[TestMethod()]
public void MinTest2()
{
DateTime x = new DateTime(2001, 1, 1, 1, 1, 2, DateTimeKind.Utc);
DateTime y = new DateTime(2001, 1, 1, 1, 1, 1, DateTimeKind.Local);
//Presumes Local TimeZone adjustment to UTC > 0
DateTime actual = DateTool.Min(x, y);
Assert.AreEqual(x, actual);
}
Note that this test would fail East of Greenwich...
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.)
// Two different dates
var date1 = new Date(2013, 05, 13);
var date2 = new Date(2013, 04, 10) ;
// convert both dates in milliseconds and use Math.min function
var minDate = Math.min(date1.valueOf(), date2.valueOf());
// convert minDate to Date
var date = new Date(minDate);
http://jsfiddle.net/5CR37/
How about a DateTime
extension method?
public static DateTime MaxOf(this DateTime instance, DateTime dateTime)
{
return instance > dateTime ? instance : dateTime;
}
Usage:
var maxDate = date1.MaxOf(date2);