Android Question converting seconds in hh:mm:ss

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Has anyone mad a function that allows the programmer to pass seconds into it and have it return just the seconds portion?

For exaple, a function called fcnNumberOfSeconds:

B4X:
Sub fcnNumberOfSeconds (intSeconds As Int) As Int
  
    Dim intReturnValue As Int = 0
  
    ' Code to extract just the seconds from the number passed in.
    '------------------------------------------------------------
  
    Return intReturnValue
End Sub

Let's say the number passed to it is 95 which is 1 minute 35 seconds. I'm looking for the function to return the number 35.

Does B4A have a time function that does this?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
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 and " & NumberFormat(seconds, 2, 0) & " seconds"
End Sub

B4X:
Dim ticks As Long = 95 * DateTime.TicksPerSecond
Log(ConvertTicksToTimeString(ticks)
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
for minute:second only use:

B4X:
Sub ConvertTicksToTimeString(t As Long) As String
   
    Dim  minutes, seconds As Int

    minutes = t/60
    seconds = t - minutes * 60
    Return NumberFormat(minutes, 2, 0) & ":" & NumberFormat(seconds, 2, 0)
   
End Sub
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
You can use this code:
B4X:
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 and " & NumberFormat(seconds, 2, 0) & " seconds"
End Sub

B4X:
Dim ticks As Long = 95 * DateTime.TicksPerSecond
Log(ConvertTicksToTimeString(ticks)

With milliseconds :)
B4X:
Private Sub ConvertTicksToTimeString2(t As Long) As String
    Dim Hours, Minutes, Seconds, Milliseconds As Int

    Hours = t / DateTime.TicksPerHour
    Minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    Seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Milliseconds = t Mod 1000
   
    Return $"$2.0{Hours}:$2.0{Minutes}:$2.0{Seconds}:$2.0{Milliseconds}"$
End Sub

(To be moved/copied in the snippet forum?)
P.S. Done, added to the original one
 
Last edited:
Upvote 0
Top