Android Question Periodbetween string and datetime.now

Sub7

Active Member
Licensed User
Longtime User
Hello, i have search on forum and tried different solutions but i am missing something.
I want to calculate the time (minutes) elapsed between two dates (date+time)

I have this date stored on remote db and in the following format: yyyy-mm-dd hh:mm:ss

I want to calculate the difference in minutes between now and this date, i did that but is not working ofc

B4X:
'sent_date = "xxxx-xx-xx xx:xx:xx"
date = sent_date.SubString2(0,10)'Get date only   
minutes = sent_date.SubString2(12,19)'Get time only
 'Convert
foo =DateTime.DateTimeParse(datediff, minutes)      
 'Time elapsed:
period1 = DateUtils.PeriodBetween(foo,DateTime.Now)
elapsed = period1.minutes&"mins"


what's wrong? ty
 

Sub7

Active Member
Licensed User
Longtime User
I just noticed i have a major problem with this, your code works perfectly but this this calculate the difference from the remotely stored datetime and local phone datetime, this mean that if the server is in UK and i am in US there the calculated time elapsed is not real.
What i could do is fetch the server time, parse date and time and instead of passing datetime.now i'll pass server time, but for some reason this is trowing me random errors.

B4X:
sent_date = m.get("sent_date") 'yyyy-mm-dd hh:mm:ss
servertime = m.get("time") 'yyyy-mm-dd hh:mm:ss
'Parse stored time
Dim minutes, datediff As String
datediff = sent_date.SubString2(0,10) 'Get date only
minutes = sent_date.SubString2(12,19)'Get time only 
'Parse server time
Dim serverday As String = servertime.SubString2(0,10)
Dim serverhour As String = servertime.SubString2(12,19)
                    'Convert
                    parsedServerTime = DateTime.DateTimeParse(serverday, serverhour)
                    sent_Datetoticks =DateTime.DateTimeParse(datediff, minutes)'Stored datetime
                    '#######################
                    'Dim ticks As Long = DateTime.Now - sent_Datetoticks 'Erel code
                    '#######################
                    Dim ticks As Long = parsedServerTime - sent_Datetoticks  'Difference between current server time.
                    Dim minutes_elapsed As Int = ticks / DateTime.TicksPerMinute
                    log(minutes_elapsed&"Mins elapsed")

Is there any other way to sync date between client/server because this is not working fine for me.

Maybe make the app to get the local timezone then make difference with server time?


ty
 
Last edited:
Upvote 0

Roberto P.

Well-Known Member
Licensed User
Longtime User
I'm using the management DateUtils.PeriodBetween to calculate the interval in hours between two dates. Shall not count the hours when you exceed one day, for example:

07/29/2015 - 10:00
30/07/2015 - 11:00

They are calculated one hour but not 25 hours

there is a way to properly calculate elapsed time?
thank you
 
Upvote 0
Top