How to validate if a DateTime field is not null / empty?

Total Post:39

Points:273
C# 
C#
 1090  View(s)
Ratings:
Rate this:

I found something like:

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

This code simply check if the name 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?

  1. Post:47

    Points:331
    Re: How to validate if a DateTime field is not null / empty?

    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
     }

      Modified On Apr-09-2018 01:21:57 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!