Adding up into the chaos!

VB Code:
  1. Public Type HourMinuteSecondMS
  2.     Hours As Integer
  3.     Mins As Byte
  4.     Secs As Byte
  5.     MS As Integer
  6. End Type
  7.  
  8. Public Function GetHMSfromMS(ByVal Milliseconds As Long) As HourMinuteSecondMS
  9.     GetHMSfromMS.Hours = CInt(Milliseconds \ 3600000)
  10.     GetHMSfromMS.Mins = CByte((Milliseconds Mod 3600000) \ 60000)
  11.     GetHMSfromMS.Secs = CByte((Milliseconds Mod 60000) \ 1000)
  12.     GetHMSfromMS.MS = CInt(Milliseconds Mod 1000)
  13. End Function
  14. Public Function GetStrFromMS(ByVal Milliseconds As Long, Optional ByVal AlwaysFull As Boolean) As String
  15.     Dim lngHours As Long, lngMins As Long, lngSecs As Long, lngMS As Long
  16.     Dim blnHours As Long, blnMins As Long
  17.     lngHours = Milliseconds \ 3600000
  18.     blnHours = (lngHours > 0) Or AlwaysFull
  19.     lngMins = (Milliseconds Mod 3600000) \ 60000
  20.     blnMins = (lngMins > 0) Or blnHours
  21.     lngSecs = (Milliseconds Mod 60000) \ 1000
  22.     lngMS = Milliseconds Mod 1000
  23.     If blnHours Then
  24.         GetHMSfromMS = CStr(lngHours) & ":" & Format$(lngMins, "00") & ":" & _
  25.             Format$(lngSecs, "00") & "." & Format$(lngMS, "000")
  26.     ElseIf blnMins Then
  27.         GetHMSfromMS = Format$(lngMins, "00") & ":" & _
  28.             Format$(lngSecs, "00") & "." & Format$(lngMS, "000")
  29.     Else
  30.         GetHMSfromMS = Format$(lngSecs, "00") & "." & Format$(lngMS, "000")
  31.     End If
  32. End Function

These are two different kinds of function. The other returns, at minimum, 00.000, but it automatically adds minutes or hours if necessary, so you end up with 00:00.000 or 0:00:00.000 formatted strings.

GetHMSfromMS instead returns a udt which contains all values splitted up nicely. This is useful if you want to make a custom digital display or something of that kind instead of using a label.