Elapsed Time Problem

Harris

Expert
Licensed User
Longtime User
Dim TM1, TM2, TM3 as long

TM1 = DateTime.Now
' wait about 10 seconds....
TM2 = DateTime.now

TM3 = TM2 - TM1

Label.text = DateTime.Time(TM3)

I get this result: 16:00:10

The minutes and seconds are correct but the hour is always 16??

How does one express elapsed time correctly?

Thanks

MHW
 

ssg

Well-Known Member
Licensed User
Longtime User
Hi,

I had the same issue when trying to develop a stop watch app.

I believe the issue is due to time zone difference.

What I did was get the adjustment time :

DateTime.TimeParse("00:00:00")

The adjustment time will tell you how many ticks you need to minus or plus to get the right time.

Try that out. Or try changing your timezone to GMT + 0 (London's time). You will notice the hour is zero.

Hope my explanation is clear.

All the best!
 
Upvote 0

LittleEddie

Member
Licensed User
Longtime User
Dim TM1, TM2, TM3 as long

TM1 = DateTime.Now
' wait about 10 seconds....
TM2 = DateTime.now

TM3 = TM2 - TM1

Label.text = DateTime.Time(TM3)

Think about it this way
.Time give you the time of day based on the ticks from 1970.
so to make it easy here your taking
1000 - 900 = 100
Now 100 is the different between 1000 and 900, now if you put that in the .Time it will give you the time 100 from 1970 not the amount of time between 1000 and 900.

Back to our question, TM3 is going to be 1000 for every second between TM1 and TM2

You can use
.GetHour(TM3)
.GetMinute(TM3)
.GetSecond(TM3)

so
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
TM1 = DateTime.Now
End Sub

Sub Button1_Click
TM2 = DateTime.Now
Label1.Text = DateTime.GetMinute(TM2-TM1) '***Get the Total number of minutes in ticks 0 to 59****
Label1.Text = Label1.Text & ":" & DateTime.GetSecond(TM2 - TM1) '***Get the Seconds in Ticks 0 to 59 (MOD)****
End Sub

Hours is 0 to 24

Displays 2:33, 2 minutes and 33 seconds after the app starts
 
Last edited:
Upvote 0

Harris

Expert
Licensed User
Longtime User
When I use .Gethour(TM3) the result is "16" (unfortunately) regardless of wht my timezone is set to (as suggested and read elsewhere obout these sorts of time issues).

TM3 is the number of seconds that increments properly when used in my timer.

What I need is a function that will return this string "d hh:mm:ss" when passed TM3. I thought I wrote this years ago but be damned if I can find it now.

Thanks to all. :sign0089:

MHW
 
Upvote 0

LittleEddie

Member
Licensed User
Longtime User
start with 'what is TM3'
Dim TM1, TM2, TM3 as long

TM1 = DateTime.Now
' wait about 10 seconds....
TM2 = DateTime.now

TM3 = TM2 - TM1

So TM3 is the number of milliseconds between TM2 and TM1

if you get a date with TM3 it would start on 12/31/69 with TM3 = 10,000 (10 seconds) DateTime.Date(TM2-TM1)

Something is wrong with .GetHour a small number will have it returning a bad number and not zero as I would expect. but once the number is over an hour it starts working correctly (1000 * 60 * 60) * 2 shows 2 hours but DateTime.GetHour(1000 * 60 * 30) shows 19 hours.

Ed
 
Last edited:
Upvote 0

LittleEddie

Member
Licensed User
Longtime User
Try this if your looking for how long the timer has been running

B4X:
Sub Button1_Click
   ButtonCount = Buttoncount + 1
   TM2 = DateTime.Now
   TM3 = TM2-TM1
   Dim TempHours As Long 
   If TM3 < DateTime.TicksPerHour Then
      TempHours = 0
   Else
      TempHours = DateTime.GetHour(TM3)
   End If
   label1.Text = "Timer has been Running for"
   label1.Text = Label1.Text & CRLF & "Days " & NumberFormat(TM3 / DateTime.TicksPerDay, 0, 0)
   Label1.Text = Label1.Text & CRLF & "Hours " & TempHours
   Label1.Text = Label1.Text & ":" & DateTime.GetMinute(TM2-TM1)
   Label1.Text = Label1.Text & ":" & DateTime.GetSecond(TM2 - TM1)
 
Last edited:
Upvote 0

Harris

Expert
Licensed User
Longtime User
"Try this if your looking for how long the timer has been running"


Thanks.
That looks like it shall work since the .getminute and .getsecond behave as expected. It is most helpful to see how others takle problem solving using B4a properties and methods.

TM1 = the last time I performed an activity - 2 minutes or 2 weeks ago. This is saved in a table.

The app runs later... TM2 = Now (inside my timer). The result shows me the exact elapsed time (inc'd every second) since the activity was last performed (saved). This is important since (by law) eight or more hours must have elapsed from the last recorded activity before a new one can (legally) start.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
So, here is the code...

Sub TTimer_Tick

Dim TM1, TM2, TM3 As Long
Dim TempHours As Long

TM1 = def.fLogDate
TM2 = DateTime.Now
TM3 = TM2-TM1
If TM3 < DateTime.TicksPerHour Then
TempHours = 0
Else
TempHours = DateTime.GetHour(TM3)
End If


StatTime.Text = "Days: "& NumberFormat(TM3 / DateTime.TicksPerDay, 0, 0)
StatTime.Text = StatTime.Text & CRLF & TempHours
StatTime.Text = StatTime.Text & ": " & DateTime.GetMinute(TM2 - TM1)
StatTime.Text = StatTime.Text & ": " & DateTime.GetSecond(TM2 - TM1)


End Sub

I started the timer at: 03/31/2011 - 20:13:24
Went to bed last night with it running..

Got up this am and checked it at: Apr 1 : 8:36am

Timer says: 1 day 4:23:05

What the ?@#$!!!

Thanks
MHW
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Hi MHW,

I am kinda curious on how this would work out.

Could you try modifying the following line in your code above:

Original:
TM3 = TM2-TM1

New:
TM3 = TM2 - TM1 + DateTime.TimeParse("00:00:00")
OR
TM3 = TM2 - TM1 - DateTime.TimeParse("00:00:00")

I just tried this out, and it did work (the one with the plus).

My curiosity is if it really is due to timezone differences.

Do let me know how this one goes ya!

Thanks and all the best!
 
Upvote 0

LittleEddie

Member
Licensed User
Longtime User
Ok I'm giving up on the GetHour stuff.

This will work, it's just math.

Label1.Text = Floor(TM3 / DateTime.TicksPerDay)
Label1.Text = Label1.Text & CRLF & (Floor(TM3 / DateTime.TicksPerHour) Mod 24)
Label1.Text = Label1.Text & ": " & (Floor(TM3 / DateTime.TicksPerMinute) Mod 60)
Label1.Text = Label1.Text & ": " & (Floor(TM3 / DateTime.TicksPerSecond) Mod 60)

You may need to change the format of the day, here it's 1.0
 
Upvote 0
Top