VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Convert Milliseconds to Time

by Aidan (3 Submissions)
Category: VB function enhancement
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (2 Votes)

Converts a number of milliseconds to a time of form HH:MM:SS:hh

Inputs
No. of Milliseconds
Code Returns
Time

Rate Convert Milliseconds to Time

' Enumerations:
Private Enum BeforeOrAfter
  Before
  After
End Enum
' ********** Procedure: Convert Milliseconds To Time **********
Public Function ConvertMillisecondsToTime(Milliseconds As Long, Optional IncludeHours As Boolean) As String
  ' Converts a number of Milliseconds to a time (HH:MM:SS:HH)
  
  Dim CurrentHSecs As Double, HSecs As Long, Mins As Long, Secs As Long, Hours As Double
  CurrentHSecs = Int((Milliseconds / 10) + 0.5)
  If IncludeHours Then
    Hours = Int(CurrentHSecs / 360000)
    CurrentHSecs = CurrentHSecs - (Hours * 360000)
  End If
  Mins = Int(CurrentHSecs / 6000)
  CurrentHSecs = CurrentHSecs - (Mins * 6000)
  Secs = Int((CurrentHSecs) / 100)
  CurrentHSecs = CurrentHSecs - (Secs * 100)
  HSecs = CurrentHSecs
  ConvertMillisecondsToTime = FixLength(Mins, 2) & ":" & FixLength(Secs, 2) & ":" & FixLength(HSecs, 2)
  If IncludeHours Then
    ConvertMillisecondsToTime = FixLength(Hours, 2) & ":" & ConvertMillisecondsToTime
  End If
End Function
' ********** Additional Subs/Functions Required **********
Private Function FixLength(Number As Variant, Length As Integer, Optional CharacterPosition As BeforeOrAfter = Before, Optional Character As String = "0") As String
  ' Inserts "0"'s before a number to make it a certain length
  Dim i As Integer, StrNum As String
  
  StrNum = CStr(Number)
  FixLength = StrNum
  For i = Len(StrNum) To Length - 1
    If CharacterPosition = Before Then
      FixLength = Character & FixLength
    Else
      FixLength = FixLength & Character
    End If
  Next i
End Function

Download this snippet    Add to My Saved Code

Convert Milliseconds to Time Comments

No comments have been posted about Convert Milliseconds to Time. Why not be the first to post a comment about Convert Milliseconds to Time.

Post your comment

Subject:
Message:
0/1000 characters