Nullable TimeSpan? to TimeSpan

18,246
TimeSpan? timestamp = DateTime.Now - modelXyz.DateStamp;
if(timestamp.HasValue)
{
    TimeSpan nonNullableTS = timestamp.Value;
}
Share:
18,246

Related videos on Youtube

Newbie2DotNet
Author by

Newbie2DotNet

Updated on July 22, 2022

Comments

  • Newbie2DotNet
    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
      Jeppe Stig Nielsen over 9 years
      What if DateStamp is actually null? Can we assume that will not happen?
  • Jeppe Stig Nielsen
    Jeppe Stig Nielsen over 9 years
    Or of course: if (modelXyz.DateStamp.HasValue) { TimeSpan nonNullableTS = DateTime.Now - modelXyz.DateStamp.Value; /* use the time span here */ }
  • Newbie2DotNet
    Newbie2DotNet over 9 years
    From another question, I got this DateTime timeStamp = modeXyz.DateStamp.GetValueOrDefault(); Is this right?
  • granadaCoder
    granadaCoder over 9 years
    Sure. You're basically trying to understand how "nullable's" work. msdn.microsoft.com/en-us/library/72cec0e0%28v=vs.110%29.aspx