我有一个DateTimeOffset?
对象,我想根据某种日期时间格式进行格式化。
我发现DateTimeOffset
类型有ToString(String,IFormatProvider)
方法:
http://msdn.microsoft.com/de-de/library/bb351892(v=vs.110).aspx
但对于DateTimeOffset?
类型的对象,它既不存在,也不能找到此类型的细节,或者如何转换它。
什么是DateTimeoffset?
,如何使用自定义格式将其转换为字符串?
答案 0 :(得分:3)
DateTimeOffset?
与Nullable<DateTimeOffset>
相同,后者具有.Value
属性,可获取基础DateTimeOffset
值:
DateTimeOffset? dto;
...
string s = dto.Value.ToString(String,IFormatProvider);
请注意,在调用DateTimeOffset
之前,您应检查以确保可空.Value
具有值,否则您将获得NullReferenceException
:
DateTimeOffset? dto;
...
if(dto.HasValue)
string s = dto.Value.ToString(String,IFormatProvider);
答案 1 :(得分:1)
DateTimeoffset?
是DateTimeoffset
的可空版本,仅此而已。您可以使用.Value
访问其值
http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx有更详细的信息。