Difference between 2 DateTimes per watch?

Possible duplicate:
Display the difference between two datetime values ​​in hours

Hi,

Is there an easy way to get the difference between 2 DateTime values ​​in hours?

I know his ability to calculate himself, to get a difference of 24h for every day, 188h for every month, and so on ... but is there an easy way given by mybe?

Example:

1) 01.02.2010 12:00 2) 03.03.2011 14:00 

=? Watches are different

+7
source share
5 answers

It is pretty simple:

 TimeSpan diff = secondDate - firstDate; double hours = diff.TotalHours; 

Note that if these DateTime values ​​were taken locally, the result may not be the number of elapsed hours. For example, you might have one DateTime midnight and one of 2 in the morning, but only one hour has passed - because the clock went ahead at 1 am. This may or may not be a problem for you. This is not a problem if you are dealing with UTC DateTime values.

+37
source

(dateTime1-dateTime2).TotalHours will give a double with the total difference between the hours between them.

+5
source
 date1.Subtract(date2).TotalHours 
+4
source
 TimeSpan difference = firstDateTime - secondDateTime; double diffInHours = difference.TotalHours 
+1
source

DateTime.Subtract (DateTime) will return a TimeSpan that has the TotalHours property.

+1
source

All Articles