如何在 C# 中检查一个 DateTime 是否大于另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/95895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if one DateTime is greater than the other in C#
提问by Brian G
I have two DateTime
objects: StartDate
and EndDate
. I want to make sure StartDate
is before EndDate
. How is this done in C#?
我有两个DateTime
对象:StartDate
和EndDate
。我想确定StartDate
是之前EndDate
。这是如何在 C# 中完成的?
采纳答案by Darren Kopp
if (StartDate < EndDate)
// code
if you just want the dates, and not the time
如果你只想要日期,而不是时间
if (StartDate.Date < EndDate.Date)
// code
回答by Ryan Rinaldi
if(StartDate < EndDate)
{}
DateTime supports normal comparision operators.
DateTime 支持普通的比较运算符。
回答by Jon Galloway
if(dateTimeA > dateTimeB) Console.WriteLine("Do your own homework");
回答by EvilEddie
Check out DateTime.Compare method
查看 DateTime.Compare 方法
回答by Rob Gray
StartDate < EndDate
回答by Ben Hoffstein
You can use the overloaded < or > operators.
您可以使用重载的 < 或 > 运算符。
For example:
例如:
DateTime d1 = new DateTime(2008, 1, 1);
DateTime d2 = new DateTime(2008, 1, 2);
if (d1 < d2) { ...
回答by Ian Nelson
if (StartDate>=EndDate)
{
throw new InvalidOperationException("Ack! StartDate is not before EndDate!");
}
回答by Loren Segal
if (new DateTime(5000) > new DateTime(1000))
{
Console.WriteLine("i win");
}
回答by John J Smith
I had the same requirement, but when using the accepted answer, it did not fulfill all of my unit tests. The issue for me is when you have a new object, with Start and End dates and you have to set the Start date ( at this stage your End date has the minimum date value of 01/01/0001) - this solution did pass all my unit tests:
我有同样的要求,但是当使用接受的答案时,它并没有满足我所有的单元测试。我的问题是当你有一个新对象,开始和结束日期,你必须设置开始日期(在这个阶段你的结束日期的最小日期值为 01/01/0001) - 这个解决方案确实通过了所有我的单元测试:
public DateTime Start
{
get { return _start; }
set
{
if (_end.Equals(DateTime.MinValue))
{
_start = value;
}
else if (value.Date < _end.Date)
{
_start = value;
}
else
{
throw new ArgumentException("Start date must be before the End date.");
}
}
}
public DateTime End
{
get { return _end; }
set
{
if (_start.Equals(DateTime.MinValue))
{
_end = value;
}
else if (value.Date > _start.Date)
{
_end = value;
}
else
{
throw new ArgumentException("End date must be after the Start date.");
}
}
}
It does miss the edge case where both Start and End dates can be 01/01/0001 but I'm not concerned about that.
它确实错过了开始和结束日期都可以是 01/01/0001 的边缘情况,但我并不担心。
回答by rottenbanana
This is probably too late, but to benefit other people who might stumble upon this, I used an extension method do to this using IComparable
like this:
这可能为时已晚,但为了让其他可能偶然发现此问题的人受益,我使用了一个扩展方法来解决这个IComparable
问题:
public static class BetweenExtension
{
public static bool IsBetween<T>(this T value, T min, T max) where T : IComparable
{
return (min.CompareTo(value) <= 0) && (value.CompareTo(max) <= 0);
}
}
Using this extension method with IComparable
makes this method more generic and makes it usable with a wide variety of data types and not just dates.
使用此扩展方法IComparable
使此方法更通用,并使其可用于多种数据类型,而不仅仅是日期。
You would use it like this:
你会像这样使用它:
DateTime start = new DateTime(2015,1,1);
DateTime end = new DateTime(2015,12,31);
DateTime now = new DateTime(2015,8,20);
if(now.IsBetween(start, end))
{
//Your code here
}