DateTime issue?

Hi!

I’ll appreciate any help you guys can give me!

I need to sum mili-seconds to a “timecode”
The timecode is this: “00:04:53,595” and i want to sum “16” mili-seconds to it.
The result should be “00:04:53,611”, right?

To avoid the need to managing sums like “23:59:59,999” + 55ms, i opted to use DateInterval to do it for me.

Here is part of my code:

Dim prev_timeCodeEnd as string = “00:04:53,595”
Dim h, m, s, ms As Integer
Dim diffTime As Integer = 16 // ms
Dim timeCode As String

h = Val(NthField(prev_timeCodeEnd,“:”,1)) // 0
m = Val(NthField(prev_timeCodeEnd,“:”,2)) // 4
s = Val(NthField(NthField(prev_timeCodeEnd,“:”,3),“,”,1)) // 53
ms = Val(NthField(NthField(prev_timeCodeEnd,“:”,3),“,”,2)) // 595

Var di As New DateInterval
di.Nanoseconds = (diffTime)*1000000 // 16000000

Var d1 As New DateTime(2025, 1, 1, h, m, s, (ms*1000000) )

and so on…

// at this point, just looking for code logic, the nanosecond of d1, should be 595000000, right?
WRONG! inspecting di.nanosecond, the value is 595000028 // why this extra 28???

What i’m doing wrong? Am i?

For timecodes avoid DateTime.
Convert all to ms, and handle the ms.
For presentation have a function to convert ms to string as “hh:mm:ss,nnn”.

1 Like

d1.nanosecond is 595000028 because is pulling the information from the d1.SecondsFrom1970 that is a double. Is the same as:

var test as Double = 1735711493.595

you will get 1735711493.5950000286102295

Doubles are not precise. You need to do as Rick said or some other way.

Alexandre's Code
Dim prev_timeCodeEnd As String = "00:04:53,595"
Dim h, m, s, ms As Integer
Dim diffTime As Integer = 16 // ms
Dim timeCode As String

h = Val(NthField(prev_timeCodeEnd,":",1)) // 0
m = Val(NthField(prev_timeCodeEnd,":",2)) // 4
s = Val(NthField(NthField(prev_timeCodeEnd,":",3),",",1)) // 53
ms = Val(NthField(NthField(prev_timeCodeEnd,":",3),",",2)) // 595

Var di As New DateInterval
di.Nanoseconds = (diffTime)*1000000 // 16000000

Var d1 As New DateTime(2025, 1, 1, h, m, s, (ms*1000000) )
//d1.nanosecond = 595000028
1 Like