Elapse time in miliseconds?

tamadon

Active Member
Licensed User
Longtime User
How do I modified the following code to find time elapsed accurate in milliseconds?

B4X:
 Dim seconds, minutes, hours, days As Int
 Dim date1, date2, elapsed As Long
 date1 = DateTime.DateParse("02/01/2013") + DateTime.TimeParse("08:00:00")
 date2 = DateTime.DateParse("03/01/2013") + DateTime.TimeParse("00:00:01")
 elapsed = date2 - date1
 seconds = Round(elapsed / 1000)
 minutes = Floor(seconds / 60) Mod  60
 hours = Floor(seconds / 3600)
 days = Floor(hours / 24)
 hours = hours Mod 24
 seconds = seconds Mod 60
 Log(days & ", " & hours & ":" & minutes & ":" & seconds)

The time differences that I am working on are pretty small, only for a couple of second each. I know I can ignore the days, hours etc but how can I convert the seconds to milliseconds?
 

klaus

Expert
Licensed User
Longtime User
Where do your two times come from ?
If you set the two times somewhere with DateTime.Now the values are already in milliseconds.
In your example code the difference is not just a few seconds but 4 hours.
The time resolution in your example is one second.
Be careful when using DateTime.TimeParse because the ticks for today are added.

Best regards.
 
Upvote 0

tamadon

Active Member
Licensed User
Longtime User
The two time differences are the interval between the clicks on a view.

For instance: FirstClick = 6561, SecondClick = 9300

Difference in milliseconds = SecondClick - FirstClick = 2739ms

What I am trying to do is track time difference with the accuracy of milliseconds.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you use FirstClick = DateTime.Now
and SecondClick = DateTime.Now
Difference = SecondClick - FirstClick
The Difference is in milliseconds.
This gives you a resolution of one millisecond.
But probably not an accuracy of one millisecond.
You could have a look at this thread.
Perhaps the Clocks library could help, I haven't tried it.

Best regards.
 
Last edited:
Upvote 0
Top