解析日期时间有问题吗?为特定格式。喜欢:
DateTime t1 = ...;
string st1 = t1.ToString(format); //<-- works
DateTime? t1 = ...;
string st1 = t1.ToString(format); //Dont work.
DateTime 没有重载方法?
解析日期时间有问题吗?为特定格式。喜欢:
DateTime t1 = ...;
string st1 = t1.ToString(format); //<-- works
DateTime? t1 = ...;
string st1 = t1.ToString(format); //Dont work.
DateTime 没有重载方法?
if (t1.HasValue)
string st1 = t1.Value.ToString(format);
使用合并运算符
DateTime? t1 = ...;
string st1 = t1 ?? t1.Value.ToString(format);
你可以这样尝试,nullabale 类型有一个叫做 hasValue 的属性 Nullable 有 Value
if (t1.HasValue)
t1.Value.ToString(yourFormat)
您应该首先检查 DateTime 是否为空
string strDate = (st1 != null ? st1.Value.ToString(format) : "n/a");