Android Question Is DateTime.Now a discrete function?

DanteS

Member
Licensed User
Longtime User
Hello, I use DateTime.Now to make decisions at different points within an application, but many times I see that the time has not changed since the last time I checked it.
In fact, in my case, it seems to change every 2 minutes and 11 seconds, just as if it were a discrete function, not a continuous function. Is this okay or am I doing something wrong?

My code is

B4X:
Sub Activity_Create(FirstTime As Boolean)
    '...
    Ticks=DateTime.Now                'Ticks takes the value of time when the program starts
    dTime=Ticks                        'dTime takes a copy o Ticks
    ...
Ens Sub

Sub DoSometing
    dt=DateTime.Now                            'checks time again
    
    If dt> dTime Then                        'if time changed
        hh=DateTime.Gethour(dt-Ticks)
        mm=DateTime.GetMinute(dt-Ticks)
        ss=DateTime.Getsecond(dt-Ticks)
        dTime=dt                            'updates the check time
    End if
End Sub
 
Solution
1. Switch to B4XPages.
2. Store the start time in B4XMainPage in the Initialize method.
B4X:
StartTime = DateTime.Now 'StartTime is a Long class global variable

3.
B4X:
Dim RunningTime As Long = DateTime.Now - StartTime
Log("Program is running for: " & ConvertMillisecondsToString(RunningTime))
'https://www.b4x.com/android/forum/threads/b4x-convert-milliseconds-to-string.89851/#content

DanteS

Member
Licensed User
Longtime User
Thanks for your answer Aeric.
What would be the correct way to get the amount of minutes and seconds since the program start?
dt is the now time (DateTime.Now) and Ticks is the start time.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Switch to B4XPages.
2. Store the start time in B4XMainPage in the Initialize method.
B4X:
StartTime = DateTime.Now 'StartTime is a Long class global variable

3.
B4X:
Dim RunningTime As Long = DateTime.Now - StartTime
Log("Program is running for: " & ConvertMillisecondsToString(RunningTime))
'https://www.b4x.com/android/forum/threads/b4x-convert-milliseconds-to-string.89851/#content
 
Upvote 0
Solution

DanteS

Member
Licensed User
Longtime User
Solved

The solution was the variable class. I was using Float. Once I changed the definition to Long, I begun to read running times of 2, 27 or 48 seconds or 1 minute and 33 seconds. Before that the minimun was always 2 min 11 sec and 4 min 22 sec
 
Upvote 0
Top