2 DateTime是否在时间之间的区别? [英] Difference between 2 DateTimes in Hours?
问题描述
可能重复:
<一href="http://stackoverflow.com/questions/4946316/showing-difference-between-two-datetime-values-in-hours">Showing以小时为单位两个日期时间值之间的差
Possible Duplicate:
Showing Difference between two datetime values in hours
您好,
有一个简单的方法来获得在时间2日期时间,值之间的差异?
is there an easy way to get the difference between 2 DateTime-Values in Hours?
我知道,它有可能来计算本身得到每天24小时的差异,每个月188H等等...但有一个简单的方法给出mybe?
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?
例如:
1) 01.02.2010 12:00
2) 03.03.2011 14:00
=?小时differnce
= ? Hours differnce
推荐答案
这是pretty的简单:
It's pretty simple:
TimeSpan diff = secondDate - firstDate;
double hours = diff.TotalHours;
请注意,如果这些的DateTime
值已采取的本地的,其结果可能不是的经过数的小时。例如,你可以有的DateTime
午夜之一,凌晨2点之一,但仅一个小时已经过去了 - 因为时钟向前走去凌晨1点。这可能会或可能不会是一个问题。这不会是一个问题,如果你处理的是UTC 的DateTime
值。
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.
这篇关于2 DateTime是否在时间之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!