Jump to content

SecondsToTime()


Info
 Share

Recommended Posts

$i = 3665.65;1 hour, 1 minute, 5seconds, 65 milliseconds

MsgBox(0, "", _SecondsToTime($i))

Func _SecondsToTime($seconds, $msCount = 2)
    Local $h, $m
    $h = _AddZero(Int($seconds / 3600))
    $seconds -= 3600 * $h
    $m = _AddZero(Int($seconds / 60))
    $seconds -= 60 * $m
    If $msCount = 0 Then
        $seconds = _AddZero(Int($seconds))
    ElseIf $msCount = 1 Then
        $seconds = _AddZero(Int($seconds)) & "." & StringTrimLeft(_AfterDot($seconds, $msCount), StringInStr(_AfterDot($seconds, $msCount), "."))
    Else
        $seconds = _AddZero(Int($seconds)) & "." & _AddZero(StringTrimLeft(_AfterDot($seconds, $msCount), StringInStr(_AfterDot($seconds, $msCount), ".")))
    EndIf
    Return $h & ":" & $m & ":" & $seconds
EndFunc   ;==>_SecondsToTime

Func _AddZero($data)
    Return StringFormat("%02d", $data)
EndFunc   ;==>_AddZero

Func _AfterDot($data, $after)
    Return StringLeft($data, StringInStr($data, ".") + $after)
EndFunc   ;==>_AfterDot

Maybe someone will search for this in the future. :blink:

Edited by Info
Link to comment
Share on other sites

Good luck in understanding the resultant StringFormat control of "%05.2f" for 2 decimal places, and "%02.0f" for no decimal places. And the role that "($iDecPlaces > 0)" plays. It is simple if you know.

$i = 3665.65 ; 1 hour, 1 minute, 5seconds, 650 milliseconds

MsgBox(0, "", _SecondsToTime($i, 2))


Func _SecondsToTime($secs, $iDecPlaces = 0)
    If $iDecPlaces < 0 Then $iDecPlaces = 0
    Return StringFormat("%02d:%02d:%0" & (2 + ($iDecPlaces > 0) + $iDecPlaces) & "." & $iDecPlaces & "f", _ ; ($iDecPlaces > 0) allows for decimal point.
            Int($secs / 3600), _ ; Hours
            Int(Mod($secs, 3600) / 60), _ ; Minutes
            $secs - (Int($secs / 3600) * 3600 + Int(Mod($secs, 3600) / 60) * 60)) ; Seconds
EndFunc ;==>_SecondsToTime
Edited by Malkey
Link to comment
Share on other sites

  • 3 weeks later...

Excellent script! I was about searching for such function a few days ago.

One suggest: what about adding a reverse function to it (time-to-seconds) to make it perfect?

Frankly, I tried and failed. :blink:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...