vba 如何在VBA中以秒为单位计算经过的时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9385689/
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
How to calculate elapsed time in seconds in VBA?
提问by phan
I have 2 strings, strStartTime and strEndTime.
我有 2 个字符串,strStartTime 和 strEndTime。
strStartTime = "12:32:54" strEndTime = "12:33:05"
strStartTime = "12:32:54" strEndTime = "12:33:05"
I want to find out how many seconds elapsed between strStartTime and strEndTime so I did this:
我想知道 strStartTime 和 strEndTime 之间经过了多少秒,所以我这样做了:
Dim dtDuration as date
dtDuration = DateDiff("s", CDate(strStartTime), CDate(strEndTime))
The result I get is dtDuration = "#1/10/1900#" in the Locals watch window.
我得到的结果是 Locals watch 窗口中的 dtDuration = "#1/10/1900#"。
Why does this happen? How do I get dtDuration to equal 11 for the 11 seconds that elapsed between the start and end times?
为什么会发生这种情况?如何在开始时间和结束时间之间经过的 11 秒内使 dtDuration 等于 11?
采纳答案by Radek
Just change variable type to Long:
只需将变量类型更改为 Long:
Dim dtDuration as Long
VBA converts numerical results of DateDiff functions into variable with date type.
VBA 将 DateDiff 函数的数值结果转换为日期类型的变量。