Home > DeveloperSection > Forums > How to check if a DateTime field is not null or empty?
Ankit Singh

Total Post:341

Points:2389
Posted on    April-01-2014 2:12 AM

 C# C# 
Ratings:


 1 Reply(s)
 10222  View(s)
Rate this:

I am very new in C# and I have a doubt.

In an application on which I am working I found something like it in the code:

if (!String.IsNullOrEmpty(u.nome))

This code simply check if the nome field of the u object is not an empty\null string.

Ok this is very clear for me, but what can I do to check it if a field is not a string but is DateTime object?



Pravesh Singh

Total Post:411

Points:2881
Supported
Posted on    April-10-2014 11:56 PM

Hi Ankit,

If you declare a DateTime, then the default value is DateTime.MinValue, and hence you have to check it like this:

DateTime dat = new DateTime();

if (dat==DateTime.MinValue)

{

    //unassigned

}

If the DateTime is nullable, well that's a different story:

 DateTime? dat = null;

 if (!dat.HasValue)

 {

     //unassigned

 }


Don't want to miss updates? Please click the below button!

Follow MindStick