Search Tools Links Login

Convert Milliseconds to Time


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

Original Author: Aidan

Inputs

No. of Milliseconds

Returns

Time

Code

' 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

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 100 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.