DateTime "null" value – Dev

The best answers to the question “DateTime "null" value” in the category Dev.


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?


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

DateTime? dt = null;


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:


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.

You can also use a nullable DateTime, like this:

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 null for reference types, but for our DateTime example it will return the same as DateTime.MinValue:


or, in more recent versions of C#,



Following way works as well

myClass.PublishDate = toPublish ? DateTime.Now : (DateTime?)null;

Please note that property PublishDate should be DateTime?


DateTime? MyDateTime{get;set;}

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