Nullable TimeSpan? to TimeSpan [closed]
Asked Answered
T

1

8

How do I convert this C# line of code to a TimeSpan from Nullable TimeSpan as DateStamp is Nullable DateTime

 TimeSpan? timestamp = DateTime.Now - modelXyz.DateStamp
Traumatism answered 19/3, 2015 at 15:48 Comment(1)
What if DateStamp is actually null? Can we assume that will not happen?Acromegaly
B
12
TimeSpan? timestamp = DateTime.Now - modelXyz.DateStamp;
if(timestamp.HasValue)
{
    TimeSpan nonNullableTS = timestamp.Value;
}
Berberine answered 19/3, 2015 at 15:51 Comment(3)
Or of course: if (modelXyz.DateStamp.HasValue) { TimeSpan nonNullableTS = DateTime.Now - modelXyz.DateStamp.Value; /* use the time span here */ }Acromegaly
From another question, I got this DateTime timeStamp = modeXyz.DateStamp.GetValueOrDefault(); Is this right?Traumatism
Sure. You're basically trying to understand how "nullable's" work. msdn.microsoft.com/en-us/library/72cec0e0%28v=vs.110%29.aspxBerberine

© 2022 - 2024 — McMap. All rights reserved.