2012-05-15 39 views

回答

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

http:// stack overflow.com/questions/1833054/how-can-i-format-a-nullable-datetime-with-tostring 我的不好。請關閉/刪除。 對不起 – VoonArt

1

你可以嘗試這樣的,nullabale類型有一個叫做hasValue的Nullable has Value

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

使用合併操作

DateTime? t1 = ...; 

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

財產應首先日期時間是否爲空或不檢查

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