2012-05-15 25 views

Trả lời

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 Lỗi của tôi. Đóng/xóa, vui lòng. Xin lỗi – VoonArt

1

bạn có thể thử như thế này, loại nullabale có tính chất gọi là hasValue Nullable has Value

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

Sử dụng liên hiệp hành

DateTime? t1 = ...; 

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

Bạn nên kiểm tra đầu tiên cho dù DateTime là null hay không

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