Android Question Mins between 2 dates/times

Discussion in 'Android Questions' started by Paul Boyle, Mar 25, 2015.

  1. Paul Boyle

    Paul Boyle Member Licensed 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.

    Code:
    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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    You don't need to call DateParse as you are already holding the date represented as ticks.

    Change str_StartTime and end time type to Long and remove the DateParse call.
     
  3. Paul Boyle

    Paul Boyle Member Licensed User

    I now have the below. It shows duration as 2.378834E7 no matter what.

    Code:
    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)
     
  4. DonManfred

    DonManfred Expert Licensed User

    Use Period object

    Code:
    Dim startT, endT As Long
        startT = 
    DateTime.Now
        endT = 
    DateTime.Now
        
    Dim p As Period = DateUtils.PeriodBetween(startT,endT)
        
    Log(p)
     
    Paul Boyle likes this.
  5. Paul Boyle

    Paul Boyle Member Licensed User


    How do i get the mins only. eg. if it was 1hr 10 mins I would like lng_Duration to return 70
    Code:
    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)
     
  6. DonManfred

    DonManfred Expert Licensed User

    Code:
    lng_Duration = (per_Duration.hours*60)+per_Duration.minutes
     
  7. Paul Boyle

    Paul Boyle Member Licensed User

    Would that then need to divided by 1000 (ticks per second)?
     
  8. DonManfred

    DonManfred Expert Licensed User

    You want the minutes? Then for 1h + 10 minutes it will be calculate 70. No need to divide this
     
  9. Mahares

    Mahares Well Known Member Licensed User

    lng_Duration=70
    The answer that Manfred gave you yields 70 minutes. You do not need to divide by 1000
     
    DonManfred likes this.
  10. Paul Boyle

    Paul Boyle Member Licensed User

    I understand that, but after a time of 6 mins its returning a value of 1188
     
  11. Paul Boyle

    Paul Boyle Member Licensed User


    My start time is showing as 1 Jan 1970 12:00, this is obviously the issue.
    End time is showing as 25 Mar 08:00 instead of 25 Mar 20:00
     
  12. DonManfred

    DonManfred Expert Licensed User

    What is returning 1188?
    What does
    Code:
    Log(per_Duration)
    print to your log? (complete...)
     
  13. Paul Boyle

    Paul Boyle Member Licensed User

    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?

    Code:
    4
    'Set date time format
            DateTime.SetTimeZone(0)
            
    DateTime.DateFormat = "d MMM yyyy hh:mm"
     
  14. Mahares

    Mahares Well Known Member Licensed User

    Instead of hh use HH
     
    Paul Boyle, DonManfred and NJDude like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice