Time format is not as expected

The code:

' ---- Variable Defined ----
Var dtRunStart As New date
Var dtRunStop As New date
Var curRunTime As Currency // 4 decimal precision...

' ---- Code In Action ----
Me.Text = modSystem.mthTime( )

// Start time for generator
dtRunStart.Hour = txtStartTime.Text.Left( 2 ).ToInteger
dtRunStart.Minute = txtStartTime.Text.Middle( 3, 2 ).ToInteger
dtRunStart.Second = txtStartTime.Text.Right( 2 ).ToInteger

// Stop time for generator
dtRunStop.Hour = txtStopTime.Text.Left( 2 ).ToInteger
dtRunStop.Minute = txtStopTime.Text.Middle( 3, 2 ).ToInteger
dtRunStop.Second = txtStopTime.Text.Right( 2 ).ToInteger

// Run time for generator...
curRunTime = dtRunStop.TotalSeconds - dtRunStart.TotalSeconds
curRunTime = (curRunTime / 60) / 60

// Display run time and make ready to be saved into the database...
txtRunTime.Text = curRunTime.ToString

The problem: While this works it displays the result in an format I do not want, and I have been perusing my old code and forums to try and find the solution.

The example of the problem:
In my program I enter the two times like so:
Start Generator Run: 08:32:28
Stop Generator Run: 10:19:42
And the run time is calculated then displayed like this → 1.787
But when I use LibreOffice Calc with the same times I get this → 1:47 ← THIS is what I want my program to display. So what am I doing incorrect or missing to get this format?

You aren’t taking your currency value (which is a number), and converting it to minutes and seconds for display.

Given a whole number of seconds you can calculate the hours, minutes, and seconds separately using integer division and the modulo operation:

' ---- Variable Defined ----
Var dtRunStart As New date
Var dtRunStop As New date
Var curRunTime As Integer ' whole number of seconds
' [...]
curRunTime = dtRunStop.TotalSeconds - dtRunStart.TotalSeconds
Dim minutes As Integer = curRunTime \ 60 ' whole number of minutes
Dim seconds As Integer = curRunTime mod 60 ' remaining seconds
Dim hours As Integer = minutes \ 60 ' whole number of hours
minutes = minutes mod 60 ' remaining minutes

// Display run time and make ready to be saved into the database...
txtRunTime.Text = Str(hours, "00") + ":" + Str(minutes, "00")

1.787 is 1 hour and .787 of hour (47 minutes and some seconds)
1:47 is 1 hour 47 minutes

Do you want to use Date? Try:

Var finalTime As New date
finalTime.TotalSeconds = dtRunStop.TotalSeconds - dtRunStart.TotalSeconds
txtRunTime.Text = finalTime.Hour.ToString + ":" + finalTime.Minute.ToString //will display 1:47

if you know that you always going to have less than 24 (my guess, not tested for other values but what you provided).

Do you want to use DateTime and DateInterval? Need help with that?

Andrew, thanks. This seems to work as I expect it to. Sorry for the delay in replying but been busy with my regular job and haven’t had much time for coding. Anyhow I implemented your suggestion and it works as expected. Thanks!

Simpler?

---- Variable Defined ----
Var dtRunStart As New date
Var dtRunStop As New date
Var curRunTime As New date
' [...]
curRunTime.TotalSeconds = dtRunStop.TotalSeconds - dtRunStart.TotalSeconds

// Display run time and make ready to be saved into the database...
txtRunTime.Text = curRunTime.SQLDateTime

That will give yyyy-mm-dd hh:mm:ss, OP only needs hh:mm
I basically did the same above but only showing hh:mm (it will not show 01:xx but 1:xx, don’t know if OP needs one or the other).

1 Like

Ah, sorry, I was looking at Andrew’s reply with all its math, and not yours, my bad!

1 Like

Don’t worry, all ok. As Date is deprecated maybe is something that some people will avoid.

2 Likes