可空的TimeSpan?到TimeSpan

时间:2015-03-19 15:48:12

标签: c# datetime nullable timespan

如何将此C#代码行从Nullable TimeSpan转换为TimeSpan 由于DateStamp是Nullable DateTime

 TimeSpan? timestamp = DateTime.Now - modelXyz.DateStamp

1 个答案:

答案 0 :(得分:5)

TimeSpan? timestamp = DateTime.Now - modelXyz.DateStamp
if(timestamp.HasValue)
{
    TimeSpan nonNullableTS = timestamp.Value;
}