time difference between two time stamps

Discussion in 'Android Questions' started by sangee, Jan 28, 2013.

  1. sangee

    sangee Member Licensed User

    [RESOLVED] time difference between two time stamps

    Hi,

    Can any one help in finding the time difference in minutes between two time stamps please...

    My time stamp looks like

    "2012-11-08 11-10-00" - which is YYYY-MM-DD HH-MM-SS

    if i have a new time stamp for example 2012-11-08 11-20-00

    If i can get the difference between the old and new time stamp would be 5 minutes.

    diffmins = oldtimestamp - newtimestap

    Thanks in advance...

    Regards,
    Sangee
     
    Last edited: Jan 28, 2013
  2. derez

    derez Expert Licensed User

    Translate the date-time strings to Ticks, using DateTimeParse.
    Subtract old from new and divide by TicksPerMinute.
     
  3. sangee

    sangee Member Licensed User

    Hello Derez,

    Can you please let me know with the code example please considering i am a :sign0104:

    Meanwhile.. I will try out too..

    Thanks,
    Sangee

    Got this in a search .. searched using the string DateTimeParse.. Erel's Code..

    This should work for me isnt it?

    Code:
    Sub DateBetweenTwoDates(D1 As String, T1 As String, D2 As String, T2 As String)
        
    Dim start, endTime, t As Long
        start = 
    DateTime.DateTimeParse(D1, T1)
        endTime = 
    DateTime.DateTimeParse(D2, T2)
        
    Dim days, hours, minutes, seconds As Int
        t = 
    Abs(endTime - start)
        days = 
    Floor(t / DateTime.TicksPerDay)
        hours = 
    Floor((t Mod DateTime.TicksPerDay) / DateTime.TicksPerHour)
        minutes = 
    Floor((t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute)
        seconds = 
    Floor((t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond)
        
        
    Log("Days = " & days)
        
    Log("Hours = " & hours)
        
    Log("Minutes = " & minutes)
        
    Log("Seconds = " & seconds)
        
    Msgbox("Days = " & days & CRLF & "Hours = " & hours & CRLF & "Minutes = " & minutes & CRLF & "Seconds = " & seconds, "OK")
        
    End Sub
     
    Last edited: Jan 28, 2013
  4. Erel

    Erel Administrator Staff Member Licensed User

    The above code should work. You will need however to break each string to a date string and a time string.
     
  5. sangee

    sangee Member Licensed User

    Thank Erel..and Derez.... this worked...

    Code:
    Public Sub CheckUptime()
    difMin =  MinutesBetweenTimes(Strvalue,CurStr)
    ToastMessageShow("Uptime is " & difMin & " Min !!!",True)
    End Sub

    Sub MinutesBetweenTimes( Time1 As String, Time2 As String)
        
    Dim start1, start2, t As Long
        start1 = 
    DateTime.TimeParse(Time1)
       start2 = 
    DateTime.TimeParse(Time2)
       
    Return Floor((start2 - start1) / DateTime.TicksPerMinute)
    End Sub
     
  6. anaylor01

    anaylor01 Well-Known Member Licensed User

    What are the values you are using for Strvalue,CurStr?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    As I wrote in the other thread you should use DateUtils.PeriodBetween.
     
  8. anaylor01

    anaylor01 Well-Known Member Licensed User

    So the user selects a time. Let's say 11:30pm. Which in code would be 2300. What is the format I have to have to make it work? If someone could give me a quick example code that would be appreciated. DateUtils.PeriodBetween
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    How will the user select the time?
     
  10. anaylor01

    anaylor01 Well-Known Member Licensed User

    Using the timedialog
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Dim p As Period = DateUtils.PeriodBetween(TimeDialog1.TimeTicks, DateTime.Now)
    Log(p)
     
  12. anaylor01

    anaylor01 Well-Known Member Licensed User

    There are 2 different timedialog boxes. The time difference will be between the two times they selected. So it won't have anything to do with NOW or timeticks.
     
  13. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Dim p As Period = DateUtils.PeriodBetween(TimeDialog1.TimeTicks, TimeDialog2.TimeTicks)
    Log(p)
     
  14. anaylor01

    anaylor01 Well-Known Member Licensed User

    Thanks Erel that worked. Now how do you parse it out to get just the hours or minutes?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    See DateUtils documentation. You don't need to parse it. Period is a type with the relevant fields.
     
  16. anaylor01

    anaylor01 Well-Known Member Licensed User

    Thanks Erel. I didn't see it in the documentation but I found it in another post.
     
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