Subtracting time

walterf25

Expert
Licensed User
Longtime User
Hello all, can anyone please help me by teaching me how to subtract two different time, let's say i start something at 10:05 and it finishes at 10:15, how can i subtract this two times? anyone?

thanks,
Walter
 

Mahares

Expert
Licensed User
Longtime User
Will this do it for you?

B4X:
Dim DateStart, DateEnd, TimeDiff As Long
DateStart = DateTime.DateParse("05/11/2012") + DateTime.TimeParse("10:05:00 AM")
DateEnd = DateTime.DateParse("05/11/2012") + DateTime.TimeParse("10:15:00 AM")
TimeDiff = DateEnd - DateStart
Msgbox(TimeDiff/1000/60,"Elapsed Minutes")
Answer: 10
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
If I may enhance the code provided by Mahares, I converted it to a function:
B4X:
Sub GetDifference(dDateStart As String, dTimeStart As String, dDateEnd As String, dTimeEnd As String) As Double

    Dim DateStart, DateEnd, TimeDiff As Long

    DateStart = DateTime.DateParse(dDateStart) + DateTime.TimeParse(dTimeStart)
    DateEnd = DateTime.DateParse(dDateEnd) + DateTime.TimeParse(dTimeEnd)
    
    TimeDiff = DateEnd - DateStart
            
    Return TimeDiff / 1000 / 60

End Sub

So you can call it like this:
B4X:
X = GetDifference("05/11/2012", "10:05:00 AM", "05/11/2012", "10:15:00 AM")
   
Msgbox("Minutes=" & X, "Result")
 
Upvote 0
Top