Hey so my database has time stamps in a weird format. I have figured out how to decode it but I am struggling to figure out how to get the current time and encode it in the same format (so that another program can read it too) in vba.
Here is an example
9.99169 = 9:59:30.84 AM
Let me explain
So first we separate the whole number (in this case 9) and that becomes the hour. It is stored in military time so I have some code to adjust that below.
We then take the decimal and multiply it by 60. So .99169 x 60 = 59.5014. That whole number is our minutes. (So 9:59 now)
Then again multiply the decimal by 60. (.5014 x 60 = 30.084) and that whole number is our seconds. (9:59:30)
Here is the only variation and I am not sure it is even needed since I don't know what this time format is called but I then do (.084 x 1000 = 84) and add a decimal into the front of it. (so 9:59:30.84)
I don't think the last step needs to be multiplied by 1000 since milliseconds are usually written as thousands but on the other hand I thought it would follow suit with the formula. (input welcome)
If anyone knows what this method is called I would love to know.
Here is the code I wrote to decode it.
Code:
Public Function TimeConvert(Time As String, Optional FieldName As String)
Dim Hours As String, Minutes As String, Seconds As String, MSeconds As String, TimePos As String, AMPM As String, i As Integer
TimePos = "Hours"
Time = Format(Time, "00.00000")
For i = 1 To Len(Time)
Select Case TimePos
Case "Hours"
If Not Right(Left(Time, i), 1) = "." Then
Hours = Left(Time, i)
Else
Time = Mid(Time, Len(Hours) + 1, Len(Time) - Len(Hours))
Time = Format(Time * 60, "00.00000")
If Hours >= 12 Then
If Hours <> 12 Then
Hours = Hours - 12
End If
AMPM = "PM"
Else
If Left(Hours, 1) = 0 Then
Hours = Right(Left(Hours, 2), 1)
End If
AMPM = "AM"
End If
'Debug.Print Time
TimePos = "Minutes"
i = 0
End If
Case "Minutes"
If Not Right(Left(Time, i), 1) = "." Then
Minutes = Format(Left(Time, i), "00")
Else
Time = Mid(Time, Len(Minutes) + 1, Len(Time) - Len(Minutes))
Time = Format(Time * 60, "00.00000")
'Debug.Print Time
TimePos = "Seconds"
i = 0
End If
Case "Seconds"
If Not Right(Left(Time, i), 1) = "." Then
Seconds = Format(Left(Time, i), "00")
Else
Time = Mid(Time, Len(Seconds) + 1, Len(Time) - Len(Seconds))
Time = Time * 1000
'Debug.Print Time
TimePos = "MSeconds"
i = 0
End If
Case "MSeconds"
MSeconds = Time
i = 100
Case Else
MsgBox "Error in TimeConvert() Function (Select Else)"
End Select
Next
TimeConvert = Hours & ":" & Minutes & " " & AMPM
Debug.Print Nz(FieldName, "Time Full Format") & ": " & Hours & ":" & Minutes & ":" & Seconds & "." & MSeconds & " " & AMPM
End Function
I am trying to figure out how to reverse the process though so I can store the current time using
Code:
time & Right(Format(Timer, "0.000"), 4)