VB icon

Convert Milliseconds to Time

Submitted on: 1/7/2015 5:59:00 PM
By: Aidan (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages)
Views: 754
     Converts a number of milliseconds to a time of form HH:MM:SS:hh
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
' Name: Convert Milliseconds to Time
' Description:Converts a number of milliseconds to a time of form HH:MM:SS:hh
' By: Aidan (from psc cd)
' Inputs:No. of Milliseconds
' Returns:Time

' Enumerations:
Private Enum BeforeOrAfter
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
FixLength = FixLength & Character
End If
Next i
End Function

Other 2 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.