[c#] Checking to see if a DateTime variable has had a value assigned

Is there an easy way within C# to check to see if a DateTime instance has been assigned a value or not?

This question is related to c# datetime

The answer is


If you don't want to have to worry about Null value issues like checking for null every time you use it or wrapping it up in some logic, and you also don't want to have to worry about offset time issues, then this is how I solved the problem:

startDate = startDate <= DateTime.MinValue.AddSeconds(1) ? keepIt : resetIt

I just check that the defaulted value is less than a day after the beginning of time. Works like a charm.

Edit 2021: If you need to check milliseconds of the beginning of time then just add ticks instead, but also maybe carbon dating is what you are really looking for. Still not sure carbon dating would even be as accurate as you need if you need accuracy to the tick.


DateTime is value type, so it can not never be null. If you think DateTime? ( Nullable ) you can use:

DateTime? something = GetDateTime();
bool isNull = (something == null);
bool isNull2 = !something.HasValue;

I generally prefer, where possible, to use the default value of value types to determine whether they've been set. This obviously isn't possible all the time, especially with ints - but for DateTimes, I think reserving the MinValue to signify that it hasn't been changed is fair enough. The benefit of this over nullables is that there's one less place where you'll get a null reference exception (and probably lots of places where you don't have to check for null before accessing it!)


Use Nullable<DateTime> if possible.


put this somewhere:

public static class DateTimeUtil //or whatever name
{
    public static bool IsEmpty(this DateTime dateTime)
    {
        return dateTime == default(DateTime);
    }
}

then:

DateTime datetime = ...;

if (datetime.IsEmpty())
{
    //unassigned
}

do you mean like so:

DateTime datetime = new DateTime();

if (datetime == DateTime.MinValue)
{
    //unassigned
}

or you could use Nullable

DateTime? datetime = null;

 if (!datetime.HasValue)
 {
     //unassigned
 }

Use Nullable<DateTime> if possible.


I generally prefer, where possible, to use the default value of value types to determine whether they've been set. This obviously isn't possible all the time, especially with ints - but for DateTimes, I think reserving the MinValue to signify that it hasn't been changed is fair enough. The benefit of this over nullables is that there's one less place where you'll get a null reference exception (and probably lots of places where you don't have to check for null before accessing it!)


do you mean like so:

DateTime datetime = new DateTime();

if (datetime == DateTime.MinValue)
{
    //unassigned
}

or you could use Nullable

DateTime? datetime = null;

 if (!datetime.HasValue)
 {
     //unassigned
 }

put this somewhere:

public static class DateTimeUtil //or whatever name
{
    public static bool IsEmpty(this DateTime dateTime)
    {
        return dateTime == default(DateTime);
    }
}

then:

DateTime datetime = ...;

if (datetime.IsEmpty())
{
    //unassigned
}

DateTime is value type, so it can not never be null. If you think DateTime? ( Nullable ) you can use:

DateTime? something = GetDateTime();
bool isNull = (something == null);
bool isNull2 = !something.HasValue;

I generally prefer, where possible, to use the default value of value types to determine whether they've been set. This obviously isn't possible all the time, especially with ints - but for DateTimes, I think reserving the MinValue to signify that it hasn't been changed is fair enough. The benefit of this over nullables is that there's one less place where you'll get a null reference exception (and probably lots of places where you don't have to check for null before accessing it!)


Use Nullable<DateTime> if possible.


do you mean like so:

DateTime datetime = new DateTime();

if (datetime == DateTime.MinValue)
{
    //unassigned
}

or you could use Nullable

DateTime? datetime = null;

 if (!datetime.HasValue)
 {
     //unassigned
 }

If you don't want to have to worry about Null value issues like checking for null every time you use it or wrapping it up in some logic, and you also don't want to have to worry about offset time issues, then this is how I solved the problem:

startDate = startDate <= DateTime.MinValue.AddSeconds(1) ? keepIt : resetIt

I just check that the defaulted value is less than a day after the beginning of time. Works like a charm.

Edit 2021: If you need to check milliseconds of the beginning of time then just add ticks instead, but also maybe carbon dating is what you are really looking for. Still not sure carbon dating would even be as accurate as you need if you need accuracy to the tick.


DateTime is value type, so it can not never be null. If you think DateTime? ( Nullable ) you can use:

DateTime? something = GetDateTime();
bool isNull = (something == null);
bool isNull2 = !something.HasValue;

I'd say the default value is always new DateTime(). So we can write

DateTime datetime;

if (datetime == new DateTime())
{
    //unassigned
}

Use Nullable<DateTime> if possible.


I just found out that GetHashCode() for an unassigned datetime is always zero. I am not sure if this is a good way to check for null datetime, because, I can't find any documentation on why this behavior is displayed.

if(dt.GetHashCode()==0)
{
    Console.WriteLine("DateTime is unassigned"); 
} 

DateTime is value type, so it can not never be null. If you think DateTime? ( Nullable ) you can use:

DateTime? something = GetDateTime();
bool isNull = (something == null);
bool isNull2 = !something.HasValue;

I just found out that GetHashCode() for an unassigned datetime is always zero. I am not sure if this is a good way to check for null datetime, because, I can't find any documentation on why this behavior is displayed.

if(dt.GetHashCode()==0)
{
    Console.WriteLine("DateTime is unassigned"); 
} 

I'd say the default value is always new DateTime(). So we can write

DateTime datetime;

if (datetime == new DateTime())
{
    //unassigned
}

I generally prefer, where possible, to use the default value of value types to determine whether they've been set. This obviously isn't possible all the time, especially with ints - but for DateTimes, I think reserving the MinValue to signify that it hasn't been changed is fair enough. The benefit of this over nullables is that there's one less place where you'll get a null reference exception (and probably lots of places where you don't have to check for null before accessing it!)