Android Question converting seconds in hh:mm:ss

rleiman

Well-Known Member
Licensed 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

Administrator
Staff member
Licensed 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)
 

Marcos Alves

Well-Known Member
Licensed 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
 
Top