vba 计算两个日期之间的时差 (DD/MM/YYY HH/MM)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41539579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate time difference between two dates (DD/MM/YYY HH/MM)
提问by сами J.D.
In Excel, I have these kind of fields (in the second and the third column):
在 Excel 中,我有这些字段(在第二列和第三列中):
Fecha/Hora inicio Fecha/Hora finalización
02/01/2017 21:32 03/01/2017 6:02
02/01/2017 6:02 03/01/2017 7:32
03/01/2017 7:38 04/01/2017 13:47
I want to know the time difference between the first and the second field in hours. For instance, in the second case, the difference is 25.5 hours.
我想知道第一个和第二个字段之间的时差(以小时为单位)。例如,在第二种情况下,差异是 25.5 小时。
I was trying to calculate it manually but I'm sure there is an automatic way.
我试图手动计算它,但我确定有一种自动方法。
回答by barrowc
VBA has a function called DateDiffwhich measures the difference between two dates in terms of a specified unit (anything from years to seconds). For this case, we will need to measure in minutes because we want to measure fractions of an hour and then divide by 60 to get back to hours. If we only wanted whole hours then we could measure in hours instead.
VBA 有一个名为DateDiff的函数,它根据指定的单位(从年到秒的任何单位)测量两个日期之间的差异。对于这种情况,我们需要以分钟为单位进行测量,因为我们想要测量一小时的分数,然后除以 60 以返回到小时。如果我们只想要整个小时,那么我们可以用小时来衡量。
The following should provide the required result:
以下应提供所需的结果:
MsgBox DateDiff("n", "02/01/2017 6:02", "03/01/2017 7:32") / 60
"n" is the interval specifier for minutes in the DateDiff function
“n”是 DateDiff 函数中分钟的间隔说明符