Time Elapsed Calulation

mariolu

Member
Licensed User
Longtime User
i am using the following code to calculate time difference:

DIFF = s.TimeNOD("20:00:00","14:00:00")

results is 359 min

DIFF = s.TimeNOD("19:59:00","14:00:00")

result is = -1080 min


using the following code procedure to calculate:

Sub TimeNOD(CurrentTime As String, OtherTime As String) As Double
Dim CurrTime, OthTime, MyNewDate As Long
CurrTime = DateTime.TimeParse(CurrentTime)
OthTime = DateTime.TimeParse(OtherTime)
NumOfMin = ((CurrTime-OthTime)/(DateTime.TicksPerMinute))
Return NumOfMin
End Sub

how do i modify this code to calculate it correctly when it reaches 8:00 pm

I couldn't get the answers from previous post!!!

Thanks
 

mariolu

Member
Licensed User
Longtime User
Correction On Post!!

correction!!!!!!

i am using the following code to calculate time difference:

DIFF = s.TimeNOD("20:00:00","14:00:00")


result is = -1080 min

DIFF = s.TimeNOD("19:59:00","14:00:00")

results is 359 min


using the following code procedure to calculate:

Sub TimeNOD(CurrentTime As String, OtherTime As String) As Double
Dim CurrTime, OthTime, MyNewDate As Long
CurrTime = DateTime.TimeParse(CurrentTime)
OthTime = DateTime.TimeParse(OtherTime)
NumOfMin = ((CurrTime-OthTime)/(DateTime.TicksPerMinute))
Return NumOfMin
End Sub

how do i modify this code to calculate it correctly when it reaches 8:00 pm

I couldn't get the answers from previous post!!!

Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code calculates the time difference between two dates:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim target As Long
    target = DateTime.DateParse("08/10/2012")
    target = target + 17 * DateTime.TicksPerHour + 30 * DateTime.TicksPerMinute
    Log(ConvertTicksToTimeString(target - DateTime.Now))
End Sub



Sub ConvertTicksToTimeString(t As Long) As String
    Dim days, hours, minutes, seconds As Int
    days = t / DateTime.TicksPerDay
    hours = (t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Return days & " days, " & NumberFormat(hours, 2, 0) & " hours, " _
        & NumberFormat(minutes, 2, 0) & " minutes and " & NumberFormat(seconds, 2, 0) & " seconds"
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
If you have your time in string format, which can be easily slpit into hours and minutes, and assuming that we want to calculate intraday difference in minutes (Erel's code is generic, thus more appropriate), why not simply write:
B4X:
Dim diffminutes as int
dim hour0 as int, minutes0 as int, hour1 as int, minutes1 as int
hour0=14:minute0=0:hour1=20:minute1=0
diffminutes=60*(hour1-hour0)+minute1-minute0

Just my notice :)
 
Upvote 0
Top