2012-05-15 13 views

Antwort

12
if (t1.HasValue) 
    string st1 = t1.Value.ToString(format); 
+0

http: // Stapel overflow.com/questions/1833054/how-can-i-format-a-nullable-datetime-with-tostring Mein Bad. Schließen/löschen, bitte. Entschuldigung – VoonArt

1

Sie so versuchen kann, hat nullabale Typ die Eigenschaft hasValue Nullable has Value

if (t1.HasValue) 
    t1.Value.ToString(yourFormat) 
3

Verwenden Coalesce Operator genannt

DateTime? t1 = ...; 

string st1 = t1 ?? t1.Value.ToString(format); 
0

Sie sollten zunächst prüfen, ob Datetime null ist oder nicht

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