计算两个日期之间的时差(DD / MM / YYY HH / MM)VBA(宏excel)

时间:2021-06-18 21:28:05

I'm writting a Macro (for excel) in VBA.

我正在VBA中写一个宏(for excel)。

I have these kind of fields (in the second and the third column):

我有这些领域(在第二和第三列):

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

And 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小时。

How can I do that? I was trying to calculate it manually but I'm sure there's quite an automatic way to do it. And I can't find examples with this exact format.

我怎样才能做到这一点?我试图手动计算它,但我确信这是一种非常自动的方法。我找不到这种格式的例子。

Thanks

1 个解决方案

#1


3  

VBA has a function called DateDiff which 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函数中分钟的间隔说明符

#1


3  

VBA has a function called DateDiff which 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函数中分钟的间隔说明符