### Convert Ticks Time String

Example Subs to convert ticks to a time string.
Example 1
'Convert Ticks to a time string HH:mm:ss
'Example with number of seconds as parameter:
'Dim d As Long = 60
'$"Duration ${ConvertTicksToTimeString(d * DateTime.TicksPerSecond)}"$
'Result: 00:01:00
`Sub ConvertTicksToTimeString(t As Long) As String
Dim hours, minutes, seconds As Int
hours = t / DateTime.TicksPerHour
minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
Return $"${NumberFormat(hours, 2, 0)}:${NumberFormat(minutes, 2, 0)}:${NumberFormat(seconds, 2, 0)}"$
End Sub`
Usage of Example 1:
'Convert number of seconds to a time string HH:mm:ss
'Log($"${ConvertSecondsToTimeString(3600)}"$)
'Output: 01:00:00
`Sub ConvertSecondsToTimeString(secs As Long) As String
Return ConvertTicksToTimeString(secs * DateTime.TicksPerSecond)
End Sub`
Example 2
'Converts ticks into a time string showing hours, minutes, seconds
'Example:
'Log(ConvertTicksToTimeString(101 * DateTime.TicksPerSecond))
'Result: 00 hours, 01 minutes, 41 seconds
`Sub ConvertTicksToTimeString(t As Long) As String
Dim hours, minutes, seconds As Int
hours = t / DateTime.TicksPerHour
minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
Return NumberFormat(hours, 2, 0) & " hours, " & _
NumberFormat(minutes, 2, 0) & " minutes, " & _
NumberFormat(seconds, 2, 0) & " seconds"
End Sub`

Changed: , Category: DateTime, File: T6C183M1322.htm, ID: 1322