Showing Difference between two datetime values in hours

reggie picture reggie · Feb 9, 2011 · Viewed 482.1k times · Source

I am retrieving two date time values from the database. Once the value is retrieved, I need the difference between the two values. For that, I create a timespan variable to store the difference of the 2 date values.

TimeSpan? variable = datevalue1 - datevalue2;

Now i need to show the difference which is stored in the Timespan variable in terms of number of hours. I referred to TimeSpan.TotalHours but couldn't apply the same for some reason. How do I do that? I am using C# on a MVC project. I simple need to show the difference value in hours?

EDIT: Since timespan was nullable, i couldn't use the total hours property. Now I can use it by doing TimeSpanVal.Value.TotalHours;

Answer

MarkKGreenway picture MarkKGreenway · Feb 9, 2011

you may also want to look at

var hours = (datevalue1 - datevalue2).TotalHours;