Possible Duplicate:
Showing 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, its a possibility to calculate itself by get for each day difference 24h, for each month 188h and so on... but is there an easy way given mybe?
Example:
1) 01.02.2010 12:00
2) 03.03.2011 14:00
= ? Hours differnce
It's pretty simple:
TimeSpan diff = secondDate - firstDate;
double hours = diff.TotalHours;
Note that if these DateTime
values have been taken locally, the results may not be the number of elapsed hours. For example, you could have one DateTime
of midnight and one of 2am, but only one hour had elapsed - because the clocks went forward at 1am. This may or may not be an issue for you. It won't be a problem if you're dealing with UTC DateTime
values.
(dateTime1-dateTime2).TotalHours
will give a double with the total difference in hours between the two.
date1.Subtract(date2).TotalHours
TimeSpan difference = firstDateTime - secondDateTime;
double diffInHours = difference.TotalHours
DateTime.Subtract(DateTime) will return a TimeSpan that has a TotalHours property.
精彩评论