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
Just subtract one from the other. You get a timedelta object with the difference. Save this answer.
get time() -startDate. gettime())/1000; Log. d("App","difference in hour is"+diff/1000/60/60); Mins = diff/1000/60; Seconds = diff/1000; Using this code I'm getting hours as a correct value.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With