DateTime? .ToString (format)

Possible duplicate:
How to format a null DateTime using ToString ()?

appeared parsing DateTime? in a specific format. How:

 DateTime t1 = ...;
 string st1 = t1.ToString(format); //<-- works

 DateTime? t1 = ...;
 string st1 = t1.ToString(format); //Dont work.

No overload method for DateTime?

+5
source share
4 answers
if (t1.HasValue)
    string st1 = t1.Value.ToString(format);
+12
source

Use Coalesce statement

DateTime? t1 = ...;

string st1 = t1 ?? t1.Value.ToString(format);
+3
source

you can try it like this: nullabale type has hasValue property Nullable has value

if (t1.HasValue)
   t1.Value.ToString(yourFormat)
+1
source

First you need to check if DateTime is null or not.

 string strDate = (st1 != null ? st1.Value.ToString(format) : "n/a");
0
source

All Articles