C# 日期时间“空”值

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

DateTime "null" value

c#datetimenull

提问by Mats

I've been searching a lot but couldn't find a solution. How do you deal with a DateTime that should be able to contain an uninitialized value (equivalent to null)? I have a class which might have a DateTime property value set or not. I was thinking of initializing the property holder to DateTime.MinValue, which then could easily be checked. I guess this is a quite common question, how do you do that?

我一直在搜索,但找不到解决方案。您如何处理应该能够包含未初始化值(相当于 null)的 DateTime?我有一个可能设置了 DateTime 属性值的类。我正在考虑将属性持有者初始化为 DateTime.MinValue,然后可以轻松检查。我想这是一个很常见的问题,你是怎么做到的?

采纳答案by Joel Coehoorn

For normal DateTimes, if you don't initialize them at all then they will match DateTime.MinValue, because it is a value type rather than a reference type.

对于普通的 DateTimes,如果您根本不初始化它们,那么它们将匹配DateTime.MinValue,因为它是值类型而不是引用类型。

You can also use a nullable DateTime, like this:

您还可以使用可为空的 DateTime,如下所示:

DateTime? MyNullableDate;

Or the longer form:

或者更长的形式:

Nullable<DateTime> MyNullableDate;

And, finally, there's a built in way to reference the default of any type. This returns nullfor reference types, but for our DateTime example it will return the same as DateTime.MinValue:

最后,有一种内置方式可以引用任何类型的默认值。这将返回null引用类型,但对于我们的 DateTime 示例,它将返回与以下相同的内容DateTime.MinValue

default(DateTime)

or, in more recent versions of C#,

或者,在更新的 C# 版本中,

default

回答by David Mohundro

I'd consider using a nullable types.

我会考虑使用可空类型

DateTime? myDateinstead of DateTime myDate.

DateTime? myDate而不是DateTime myDate.

回答by Mark Ingram

If you're using .NET 2.0 (or later) you can use the nullable type:

如果您使用 .NET 2.0(或更高版本),您可以使用可空类型:

DateTime? dt = null;

or

或者

Nullable<DateTime> dt = null;

then later:

后来:

dt = new DateTime();

And you can check the value with:

您可以使用以下方法检查该值:

if (dt.HasValue)
{
  // Do something with dt.Value
}

Or you can use it like:

或者你可以像这样使用它:

DateTime dt2 = dt ?? DateTime.MinValue;

You can read more here:
http://msdn.microsoft.com/en-us/library/b3h38hb0.aspx

您可以在此处阅读更多信息:http:
//msdn.microsoft.com/en-us/library/b3h38hb0.aspx

回答by Aaron Smith

You can use a nullable class.

您可以使用可为空的类。

DateTime? date = new DateTime?();

回答by Patrik Svensson

You can use a nullable DateTime for this.

您可以为此使用可为空的 DateTime。

Nullable<DateTime> myDateTime;

or the same thing written like this:

或者像这样写的同样的东西:

DateTime? myDateTime;

回答by Patrick Desjardins

I always set the time to DateTime.MinValue. This way I do not get any NullErrorException and I can compare it to a date that I know isn't set.

我总是将时间设置为DateTime.MinValue。这样我就不会收到任何 NullErrorException 异常,我可以将它与我知道未设置的日期进行比较。

回答by user29958

You can set the DateTime to Nullable. By default DateTime is not nullable. You can make it nullable in a couple of ways. Using a question mark after the type DateTime? myTime or using the generic style Nullable.

您可以将 DateTime 设置为 Nullable。默认情况下 DateTime 不可为空。您可以通过几种方式使其可以为空。在类型 DateTime 后使用问号?myTime 或使用通用样式 Nullable。

DateTime? nullDate = null;

or

或者

DateTime? nullDate;

回答by Iman

DateTime? MyDateTime{get;set;}

约会时间?MyDateTime{get;set;}

MyDateTime = (dr["f1"] == DBNull.Value) ? (DateTime?)null : ((DateTime)dr["f1"]);

回答by DarkoM

If you are, sometimes, expecting null you could use something like this:

如果您有时希望为 null,则可以使用以下内容:

var orderResults = Repository.GetOrders(id, (DateTime?)model.DateFrom, (DateTime?)model.DateTo)

In your repository use null-able datetime.

在您的存储库中使用可为空的日期时间。

public Orders[] GetOrders(string id, DateTime? dateFrom, DateTime? dateTo){...}

回答by uncoder

It is worth pointing out that, while a DateTimevariable cannot be null, it still can be compared to nullwithout a compiler error:

值得指出的是,虽然DateTime变量不能是null,但它仍然可以与null没有编译器错误的情况进行比较:

DateTime date;
...
if(date == null) // <-- will never be 'true'
  ...