在两个日期之间获得最小(或最大)值的最快捷最简单的方法是什么?是否有相当于Math.Min(&Math.Max)的日期?
我想做的事情如下:
if (Math.Min(Date1, Date2) < MINIMUM_ALLOWED_DATE) {
//not allowed to do this
}
Run Code Online (Sandbox Code Playgroud)
显然上面的Math.Min不起作用,因为它们是日期.
Meh*_*ari 435
没有内置的方法来做到这一点.您可以使用以下表达式:
(date1 > date2 ? date1 : date2)
Run Code Online (Sandbox Code Playgroud)
找到两者中的最大值.
您可以编写一个通用方法来计算Min或Max任何类型(假设Comparer<T>.Default设置得当):
public static T Max<T>(T first, T second) {
if (Comparer<T>.Default.Compare(first, second) > 0)
return first;
return second;
}
Run Code Online (Sandbox Code Playgroud)
您也可以使用LINQ:
new[]{date1, date2, date3}.Max()
Run Code Online (Sandbox Code Playgroud)
Guf*_*ffa 382
DateTime值没有重载,但您可以获取Ticks值包含的长值,比较它们,然后从结果中创建新的DateTime值:
new DateTime(Math.Min(Date1.Ticks, Date2.Ticks))
Run Code Online (Sandbox Code Playgroud)
(请注意,DateTime结构还包含一个Kind属性,该属性不会保留在新值中.这通常不是问题;如果比较不同类型的DateTime值,则无论如何比较都没有意义.)
Mar*_*ell 35
怎么样:
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;
}
Run Code Online (Sandbox Code Playgroud)
适用于任何支持IComparable<T>或支持的类型IComparable.
实际上,使用LINQ,另一种选择是:
var min = new[] {x,y,z}.Min();
Run Code Online (Sandbox Code Playgroud)
小智 20
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这允许日期具有不同的"种类"并返回传入的实例(不返回从Ticks或Milliseconds构造的新DateTime).
[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);
}
Run Code Online (Sandbox Code Playgroud)
请注意,此测试将在格林威治东部失败...
Wil*_*ler 19
对于任何想知道之前提供的一些答案的性能的人,我对其中一些答案进行了基准测试。我使用三元运算符、LINQ 和 Math.max 来确定速度和内存的最佳解决方案。
return a > b ? a : b;
Run Code Online (Sandbox Code Playgroud)
return new DateTime(Math.Max(a.Ticks, b.Ticks));
Run Code Online (Sandbox Code Playgroud)
return new[]{a, b}.Max();
Run Code Online (Sandbox Code Playgroud)
.Net版本:6.0
C# 版本:10.0
配置:发布
使用BenchmarkDotNet 0.13.5完成基准测试
三元运算符是迄今为止性能最高的方法。它的执行速度比亚军快 100 倍以上,并且不分配额外的内存。
Tos*_*shi 15
Linq.Min()/ Linq.Max()方法:
DateTime date1 = new DateTime(2000,1,1);
DateTime date2 = new DateTime(2001,1,1);
DateTime minresult = new[] { date1,date2 }.Min();
DateTime maxresult = new[] { date1,date2 }.Max();
Run Code Online (Sandbox Code Playgroud)
R. *_*ury 13
如果你想更像Math.Max,你可以做这样一个非常短的表达体:
public static DateTime Max(params DateTime[] dates) => dates.Max();
[...]
var lastUpdatedTime = DateMath.Max(feedItemDateTime, assemblyUpdatedDateTime);
Run Code Online (Sandbox Code Playgroud)
扩展方法怎么样DateTime?
public static DateTime MaxOf(this DateTime instance, DateTime dateTime)
{
return instance > dateTime ? instance : dateTime;
}
Run Code Online (Sandbox Code Playgroud)
用法:
var maxDate = date1.MaxOf(date2);
Run Code Online (Sandbox Code Playgroud)