C# 比较可为空的 DateTime?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13673117/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:24:09  来源:igfitidea点击:

Comparing nullable DateTime?

c#c#-4.0datetime

提问by genxgeek

Looking for a better way to compare a nullable date time than the following:

寻找比以下更好的方法来比较可为空的日期时间:

Any suggestions?

有什么建议?

// myobject.ExpireDatetime is of DateTime?
//
if (!myobject.ExpireDateTime.IsNull() && DateTime.Compare((DateTime)myobject.ExpireDateTime, DateTime.Now.ToUniversalTime()) < 0)
{ //error! }    

Edited: Sorry for confusion...myobject.ExpireDatetime is of type DateTime.

编辑:抱歉混淆...myobject.ExpireDatetime 是 DateTime 类型。

采纳答案by Jeppe Stig Nielsen

Your question is not quite clear to me, but if we have

你的问题对我来说不是很清楚,但如果我们有

DateTime? ExpireDateTime;  // could be a variable or a property

it's OK to say just

可以说只是

if (ExpireDateTime < DateTime.UtcNow)
{
  ...
}

This will be OK if ExpireDateTimeis null(HasValueis false). Some inexperienced developers will struggle to understand lifted operators, so to make it more clear, you could write

如果ExpireDateTimenull(HasValue是假的) 就可以了。一些没有经验的开发人员会很难理解提升操作符,所以为了更清楚,你可以写

if (ExpireDateTime < (DateTime?)DateTime.UtcNow)
{
  ...
}

It's the same, but easier to read and understand.

它是一样的,但更容易阅读和理解。

Neverwrite .Valueif the nullable might be null, of course. You will get an InvalidOperationException"Nullable object must have a value"if you do so.

.Value当然,如果可空对象可能为空,则永远不要写。如果你这样做,你会得到一个InvalidOperationException“可空对象必须有一个值”

回答by Oded

Use the Valueproperty of the nullable:

使用Value可为空的属性:

objet.ExpireDateTime.Value


if (!object.ExpireDateTime.IsNull() 
    && DateTime.Compare(objet.ExpireDateTime.Value, 
                        DateTime.Now.ToUniversalTime()) < 0)
{ 
}    

回答by Tim Schmelter

If ExpireDateTimeis a Nullable<DateTime>i would do following instead:

如果 ExpireDateTime是一个 Nullable<DateTime>我会做以下:

if (ExpireDateTime.HasValue && ExpireDateTime < DateTime.Now.ToUniversalTime())
{ 
}

回答by labroo

I would recommend you to use the following:

我建议您使用以下方法:

int y = Nullable.Compare<DateTime>(DateTime.UtcNow, x); 

// x is the nullable date time object.
// y will be '-1' if x is greater than DateTime.UtcNow

回答by Patrick

The compiler liftsvariables and generates code to check for nulls.

编译器提升变量并生成代码以检查空值。

> new DateTime?()
null
> DateTime.Now > null
false
> DateTime.Now < null
false

> new int?()
null
> 10 >= null
false
> 10 =< null
false
> 10 == null
false
> 10 != null
true

Knowing this you can write simple code.

知道了这一点,您就可以编写简单的代码。

// d is a DateTime? 
DateTime now = DateTime.Now;

bool after = d > now;
bool before = d < now;
bool today = d?.Date == now.Date;

If d is null everything will be false, else it will work like normal DateTime comparison.

如果 d 为空,一切都将是假的,否则它将像正常的 DateTime 比较一样工作。