Android Question Time format & milliseconds

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to calculate the time difference with millisecond:
B4X:
Sub Process_Globals
   Type PeriodWithMs (p As Period, ms As Int)
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Dim time1 As Long = DateTime.Now
   Dim time2 As Long = DateTime.TimeParse("23:40:00")
   Dim pm As PeriodWithMs = PeriodBetweenWithMilliseconds(time1, time2)
   Log(FN(pm.p.Hours) & ":" & FN(pm.p.Minutes) & ":" & FN(pm.p.Seconds) & "." & _
     NumberFormat(pm.ms, 3, 0))
End Sub

Sub FN(n As Int) As String
   Return NumberFormat(n, 2, 0)
End Sub

Sub PeriodBetweenWithMilliseconds(time1 As Long, time2 As Long) As PeriodWithMs
   Dim p As Period = DateUtils.PeriodBetween(time1, time2)
   Dim ms As Int = (time2 - time1) Mod DateTime.TicksPerSecond
   If ms < 0 Then ms = ms + 1000
   Dim pm As PeriodWithMs
   pm.p = p
   pm.ms = ms
   Return pm
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…