Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the total hours in a TimeSpan

Tags:

c#

timespan

How can I subtract two dates and get the total hours of the TimeSpan object that is returned?

For example, if the TimeSpan is 2 days, the total hours are 48.

like image 963
Mark Avatar asked Feb 27 '23 20:02

Mark


1 Answers

Then you want the TotalHours property of the TimeSpan object:

DateTime today = DateTime.Today;
DateTime twoDaysAgo = today.AddDays(-2.0);

// returns 48.0
double totalHours = (today - twoDaysAgo).TotalHours;
like image 99
Dan Tao Avatar answered Mar 06 '23 13:03

Dan Tao