vb6 - How can I find the difference between two times that have milliseconds -


using vb6 only...

i have 2 times , need time between them. have file contains times in format "08:34:45:734" , need calculate time difference between 2 values.

example:

08:34:12:744 08:34:45:734 

i need output generate correct time, including milliseconds, between 2 times. tried convert double , subtract, can't seem way h:m:s:m time format need.

this should work. vb dates not include number of ms strip ms off , calculate difference yourself.

private function gettimediff(strdate1 string, strdate2 string) string      dim fneg boolean     dim dt1 date, dt2 date     dim ms1 long, ms2 long      ' assign earliest date dt1...     if strdate1 <= strdate2         dt1 = left$(strdate1, 8)         dt2 = left$(strdate2, 8)         ms1 = right$(strdate1, 3)         ms2 = right$(strdate2, 3)     else         dt1 = left$(strdate2, 8)         dt2 = left$(strdate1, 8)         ms1 = right$(strdate2, 3)         ms2 = right$(strdate1, 3)         fneg = true     end if      ' if ms of starting time > ms of ending time, add second before subtraction...     dim ms long     if ms1 > ms2         dt1 = dateadd("s", 1, dt1)         ms = (1000 - ms1) + ms2     else         ms = ms2 - ms1     end if      ' subtract dates, difference in seconds...     dim h long, m long, s long     s = datediff("s", dt1, dt2)      ' convert seconds h:m:s...     h = s \ 3600     s = s mod 3600     m = s \ 60     s = s mod 60      gettimediff = iif(fneg, "-", "") & h & ":" & m & ":" & s & ":" & ms  end function 

call passing string values , return h:m:s:ms string:

debug.print gettimediff("08:34:12:744", "08:34:45:734") ' => "0:0:32:990" 

if first date later second, you'll negative value:

debug.print gettimediff("10:34:12:744", "08:34:45:734") ' => "-1:59:27:10"     

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -