Android Question Yet another time elapsed question.

Sub7

Active Member
Licensed User
I need to calculate the minutes, hours, days elapsed from a datetime stored on server located in UK and phone date time, the server date time format is: yyyy-mm-dd hh:mm:ss
Since the app is distributed worldwide, the elapsed time should not be affected by local phone timezone for this reason i have set: Datetime.SetTimeZone(1) but im not sure if this code is correct or is gonna fail somehow.

B4X:
           DateTime.SetTimeZone(1)
           Dim DateString = M.Get("sent_date") As String
           Dim DateTimeTicks As Long
           Dim Date_Time() As String
           Date_Time = Regex.Split(" ", DateString)
           DateTime.DateFormat = "yyyy-MM-dd"
           DateTime.TimeFormat = "HH:mm:ss"
           DateTimeTicks = DateTime.DateTimeParse(Date_Time(0), Date_Time(1))
           Dim ticks As Long = DateTime.Now - DateTimeTicks
           Dim minutes As Int = ticks / DateTime.TicksPerMinute

l.
 

Sub7

Active Member
Licensed User
Something like this?

B4X:
          'Server
                    Dim DateTimeTicksSRV As Long
                    Dim DateStringSRV = m.Get("date") As String
                    Dim Date_TimeSRV() As String
                    Date_TimeSRV = Regex.Split(" ", DateStringSRV)
                    DateTime.DateFormat = "yyyy-MM-dd"
                    DateTime.TimeFormat = "HH:mm:ss"
               

                    'Local
                    Dim DateTimeTicksLOCAL As Long
                    Dim LocalDateTime = DateTime.Date(DateTime.Now)&" "&DateTime.Time(DateTime.Now) As String
                    Dim Date_TimeLOCAL() As String
                    Date_TimeLOCAL = Regex.Split(" ", LocalDateTime)
               
               
                    DateTimeTicksSRV = DateTime.DateTimeParse(Date_TimeSRV(0), Date_TimeSRV(1))
                    DateTimeTicksLOCAL = DateTime.DateTimeParse(Date_TimeLOCAL(0), Date_TimeLOCAL(1))

                    Dim period1 As Period
                    period1 = DateUtils.PeriodBetween(DateTimeTicksSRV,DateTimeTicksLOCAL)
               

 
  Dim TimeElapsed As String
           If period1.Minutes < 3 Then
           TimeElapsed = "Just now!"         
           Else If period1.Minutes > 3 AND period1.Minutes < 60 AND period1.Hours = 1 Then
           TimeElapsed = period1.Minutes &" Min"
           Else If period1.Hours > 1 Then
           TimeElapsed = period1.hours &" Hours"
           Else If period1.Days >= 1 AND period1.Months < 1 Then
           TimeElapsed = period1.Days &" Days"
           Else If period1.Months >= 1 Then
           TimeElapsed = period1.Months &" Months"
           End If

Not sure if this work correctly, i am abit confused.
 
Last edited:
Upvote 0

Sub7

Active Member
Licensed User
This dirty thing seems to work, not sure is this whole thing will works worlwide

B4X:
 Dim TimeElapsed As String
                   If period1.Minutes < 3 Then
                   TimeElapsed = "Just now!"      
                   Else If period1.Minutes > 3 AND period1.Minutes < 60 AND period1.Hours = 1 AND period1.Months < 1 AND period1.Months < 12 Then
                   TimeElapsed = period1.Minutes &" Min"
                   Else If period1.Hours > 1 AND period1.Months < 1 AND period1.Months < 12 Then
                   TimeElapsed = period1.hours &" Hours"
                   Else If period1.Days >= 1 AND period1.Months < 1 AND period1.Months < 1 AND period1.Months < 12 Then
                   TimeElapsed = period1.Days &" Days"
                   Else If period1.Months <> 0 Then
                   TimeElapsed = period1.Months &" Months"
                   End If


Years are never calculated since entries that are 6 months old are deleted.
 
Upvote 0

Sub7

Active Member
Licensed User
Hello, yes server time is different since the date is stored as timestap on update current timestamp (which is server time not client time)


By setting DateTime.SetTimeZone(0) i get a negative value for example hours=-1 minute= -20 which is okay because in my case gmt+1 there is 1 hour difference between server time and device, but this affect also the second part of the code, which is not ok.
So how i can set and unset this?


By not setting datetime.setTimeZone and changing timezone on device i simulated to be in china, alaska, the result is wrong.

what's going on?
 
Last edited:
Upvote 0

Sub7

Active Member
Licensed User
Maybe my question wasnt clear:
i have this date on db: 2014-10-15 14:00:00
In app i want to show the time elapsed from 2014-10-15 14:00:00 and current server time for example 14:05:00
if i am in china and today is 2014-10-15 hour 23:30 the result should be 5 minutes,
if i am in jamaica and today is 2014-10-15 19:20:00 the result must still be 5 minutes.
 
Upvote 0

Sub7

Active Member
Licensed User
I'm not getting the server time itself but if neeeded i could do that.
For now i just fetch the values stored on db in this format: 0000-00-00 00:00:00
The datetime on db is stored while INSERT INTO tablename statement is executed and it is relative the local server time gmt+0 UK.
Not the device date time.

Thank you.
 
Upvote 0

Sub7

Active Member
Licensed User
So i resolved just by modify my query in this way:

B4X:
$query ="INSERT INTO table (field1,date) VALUES (".$xxx.", utc_timestamp())";

And by setting
B4X:
DateTime.SetTimeZone(0)

Works perfectly.
 
Upvote 0
Top