Android Question datetime question ( minute & second difference )

tufanv

Expert
Licensed User
Longtime User
Hello,

I am absolutely doing something wrong. I want to take the minute and second difference between the click of 2 buttons.

for the first button :
B4X:
dim time1 as long =DateTime.Time(DateTime.Now)

for the second button :
B4X:
dim time2 as long =DateTime.Time(DateTime.Now)

time1 and time 2 are correct , it displays with this format : 17:09:39 , but this gives 0 :

Log(DateUtils.PeriodBetween(time1,time2))


The time now is: 17:21:29
The time now is: 17:21:34
[IsInitialized=1, Years=0, Months=0
, Days=0, Hours=0, Minutes=0
, Seconds=0]

as it must give 0 minutes and 5 seconds. I am doing a very simple mistake somewhere but datetuils topic is not so detailed so i cant get it. Can you help out ? thanks
 

Mahares

Expert
Licensed User
Longtime User
I am absolutely doing something wrong.

You need something like this:
B4X:
Dim time1 As Long =DateTime.Now
    Dim time2 As Long =DateTime.now + 1230000  'put the second button click time here
    Dim p As Period
    p=DateUtils.PeriodBetweenInDays(time1,time2)
    Log($"$2.0{p.Minutes} mn $2.0{p.Seconds} sec  "$) 'displays 20 mn 30 sec
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You need something like this:
B4X:
Dim time1 As Long =DateTime.Now
    Dim time2 As Long =DateTime.now + 1230000  'put the second button click time here
    Dim p As Period
    p=DateUtils.PeriodBetweenInDays(time1,time2)
    Log($"$2.0{p.Minutes} mn $2.0{p.Seconds} sec  "$) 'displays 20 mn 30 sec
tried this:

B4X:
Sub lblstart_Click

If lblstart.Text.Contains("Start") Then

        Dim time1 As Long =DateTime.Now

lblstart.Text="Stop Recording"
lblstart.Color=Colors.Red
    Else
        Dim time2 As Long =DateTime.now
        Dim pl As Period
        pl=DateUtils.PeriodBetweenInDays(time1,time2)
        Log($"$2.0{pl.Minutes} mn $2.0{pl.Seconds} sec  "$) 'displays 20 mn 30 sec

    
        lblstart.Text="Start Recording"
        lblstart.Color=Colors.Rgb(58,197,0)
    End If


End Sub

when the button is clicked for the first time it is time1
and when it is clicked second time it is time2 .

I press the button second time after 3-4 seconds later but what i got is :
45 mn 43 sec
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub btnStart_Click
    starttime = DateTime.Now
End Sub

Sub btnStop_Click
    stoptime = DateTime.Now
    Dim pl As Period
    pl=DateUtils.PeriodBetweenInDays(starttime,stoptime)
    lblResult.Text = pl
    Log(pl)
End Sub

B4X:
[Days=0, Hours=0, IsInitialized=true
, Minutes=0, Months=0, Seconds=7
, Years=0]
 

Attachments

  • timecalc.zip
    9.1 KB · Views: 262
Upvote 0

tufanv

Expert
Licensed User
Longtime User
B4X:
Sub btnStart_Click
    starttime = DateTime.Now
End Sub

Sub btnStop_Click
    stoptime = DateTime.Now
    Dim pl As Period
    pl=DateUtils.PeriodBetweenInDays(starttime,stoptime)
    lblResult.Text = pl
    Log(pl)
End Sub

B4X:
[Days=0, Hours=0, IsInitialized=true
, Minutes=0, Months=0, Seconds=7
, Years=0]
Dim time1 As Long =DateTime.Now needs to be outside the If loop.
B4X:
Sub lblstart_Click
Dim time1 As Long =DateTime.Now
Thanks for the help guys. I didn't know that if it was in the if loop , will not work properly. Regards
 
Upvote 0
Top