DateTime "Null" = 01/01/0001

2

How to treat a DateTime field that comes as "null" (I know that DateTime can not be null ) of a legacy base?

Querying a WebService returns a DateTime field as 01/01/0001 (no value), in the case of an empty% or null% co exists there is an "elegant" way to treat string

My question is: What is the best way to treat a IsNullOrEmpty "nullo", that is, with DateTime value

    
asked by anonymous 16.10.2015 / 05:01

1 answer

5

You can compare the date with DateTime.MinValue to see if the date was instantiated with no value:

DateTime suaData = new DateTime();

if (suaData == DateTime.MinValue)
{
    //operação
}

It's worth remembering that DateTime can be used as nullable . To do this, simply add a ? after the type, in which case you can check the != null or use the HasValue attribute as below:

DateTime? suaData = null;

if (!suaData.HasValue)
{
    //operação
}
    
16.10.2015 / 12:55