Nullable TimeSpan? to TimeSpan
18,246
TimeSpan? timestamp = DateTime.Now - modelXyz.DateStamp;
if(timestamp.HasValue)
{
TimeSpan nonNullableTS = timestamp.Value;
}
Related videos on Youtube
Author by
Newbie2DotNet
Updated on July 22, 2022Comments
-
Newbie2DotNet almost 2 years
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
-
Jeppe Stig Nielsen over 9 yearsWhat if
DateStamp
is actually null? Can we assume that will not happen?
-
-
Jeppe Stig Nielsen over 9 yearsOr of course:
if (modelXyz.DateStamp.HasValue) { TimeSpan nonNullableTS = DateTime.Now - modelXyz.DateStamp.Value; /* use the time span here */ }
-
Newbie2DotNet over 9 yearsFrom another question, I got this DateTime timeStamp = modeXyz.DateStamp.GetValueOrDefault(); Is this right?
-
granadaCoder over 9 yearsSure. You're basically trying to understand how "nullable's" work. msdn.microsoft.com/en-us/library/72cec0e0%28v=vs.110%29.aspx