How to calculate elapsed time in seconds in VBA?

I have 2 lines, strStartTime and strEndTime.

strStartTime = "12:32:54" strEndTime = "12:33:05"

I want to know how many seconds elapsed between strStartTime and strEndTime, so I did this:

Dim dtDuration as date dtDuration = DateDiff("s", CDate(strStartTime), CDate(strEndTime)) 

The result I get is dtDuration = "# 1/10/1900 #" in the locale viewport.

Why is this happening? How to get dtDuration equal to 11 in 11 seconds elapsed between the start and end time?

+6
source share
1 answer

Just change the type of the variable to Long:

 Dim dtDuration as Long 

VBA converts the numeric results of DateDiff functions into a variable with a date type.

+6
source

Source: https://habr.com/ru/post/909020/


All Articles