Android Question Mins between 2 dates/times

Paul Boyle

Member
Licensed User
Longtime User
I'm trying to calculate the mins between two date/times then use the value to be entered into my database.

A button is pressed to start a game. Start Time is recorded. A different button is pressed to end a game, End Time is recorded. I want to calc the mins between the Start Time and the End Time.

B4X:
Sub Globals
     Dim str_StartTime as String, str_EndTime as String, str_Duration as String
End Sub

Sub btn_Start_click
     str_StartTime = DateTime.Now
End Sub

sub btn_End_click
    str_EndTime = DatTime.Now
End Sub

Sub Duration (StartTime as String, EndTime as String)
     DateTime.SetTimeZone(0)
     DateTime.DateFormat = "d MMM yyyy hh:mm"

     Return (DateTime.DateParse(str_EndTime) - DateTime.DateParse(str_StartTime)) / DateTime.TicksPerMinute

End Sub


INSERT INTO table VALUES(str_Duration)


Getting an error of 'Unparseable date: "1427298075389" (at offset 13)'

Many Thanks

P
 

Paul Boyle

Member
Licensed User
Longtime User
I now have the below. It shows duration as 2.378834E7 no matter what.

B4X:
Sub Globals
     Dim str_StartTime as Long, str_EndTime as Long, str_Duration as String
End Sub

Sub btn_Start_click
     str_StartTime = DateTime.Now
End Sub

sub btn_End_click
    str_EndTime = DateTime.Now
End Sub

Sub Duration (StartTime as String, EndTime as String)
     DateTime.SetTimeZone(0)
     DateTime.DateFormat = "d MMM yyyy hh:mm"

     Return (((str_EndTime) - str_StartTime) / DateTime.TicksPerMinute))

End Sub

str_Duration = Duration(str_StartTime,strEndTime) & " mins"

INSERT INTO table VALUES(str_Duration)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Use Period object

B4X:
    Dim startT, endT As Long
    startT = DateTime.Now
    endT = DateTime.Now
    Dim p As Period = DateUtils.PeriodBetween(startT,endT)
    Log(p)
 
Upvote 0

Paul Boyle

Member
Licensed User
Longtime User
Use Period object

B4X:
    Dim startT, endT As Long
    startT = DateTime.Now
    endT = DateTime.Now
    Dim p As Period = DateUtils.PeriodBetween(startT,endT)
    Log(p)


How do i get the mins only. eg. if it was 1hr 10 mins I would like lng_Duration to return 70
B4X:
Dim per_Duration As Period

per_Duration = DateUtils.PeriodBetween(lng_StartTime,lng_EndTime)
Log(per_Duration)

lng_Duration = per_Duration.Minutes

INSERT INTO table VALUES (lng_Duration)
 
Upvote 0

Paul Boyle

Member
Licensed User
Longtime User
What is returning 1188?
What does
B4X:
Log(per_Duration)
print to your log? (complete...)

Its working now. The startTime variable is inside an IF statement, and wasnt being triggered and defaulted to 1 Jan 1970.

How do I get the below to return 24hr format?

B4X:
4
'Set date time format
        DateTime.SetTimeZone(0)
        DateTime.DateFormat = "d MMM yyyy hh:mm"
 
Upvote 0
Top