How to display time calculations in: "hh:mm:ss" format

RunTime91

Active Member
Joined
Aug 30, 2012
Messages
298
Office Version
  1. 365
Hello All...


I can make the following code provide the number of seconds between two times but I can't seem to translate those seconds into a "hh:mm:ss" format.

What am I doing wrong?

Code:
Dim Time1 As Date
Dim Time2 As Date
Dim TimeDiff As Double

Time1 = Me.txt22.Value
Time2 = Me.txt23.Value

TimeDiff = (Time2 - Time1) *86400

Me.lbl1 = Format(TimeDiff, "hh:mm:ss")

TimeDiff calculates the correct # of seconds but when I attempt to format those seconds into the "hh:mm:ss" format it shows 00:00:00

No amount of google or youtube reveals an answer, so..... here I am

Thank You in Advance...

RT91
 
Further to the post above:

PS. Having played further with the VBA Format statement I realise that it has some anomalies, which also explain Alex's comments further:
1. It doesn't handle durations in excess of 24hrs (using [hh].
2. Format statements such as "mm:ss" return a strange number. It needs "h:mm:ss" or "hh:mm:ss".
3. It doesn't handle negative times, even if the 1904 option is selected.

Hope this helps.
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Greetings All and Thank You to those who have chimed-in on this thread.
Here is the latest which is sure to beg more questions than provide answers.

To those who are saying 'Remove the *86400' The solution (thus far) seems to indicate this is correct advice.
Problem is, when I remove it, the code returns 00:00:00 as an answer.

This code returns the answer mentioned above (00:00:00)
Code:
Dim Time1 As Date
Dim Time2 As Date
Dim TimeDiff As Integer '--> This was previously 'Double'

Time1 = Me.txt22.Value
Time2 = Me.txt23.Value
TimeDiff = (Time2 - Time1)
Me.lbl1 = Format(TimeDiff, "hh:mm:ss")

While this code consistently returns the correct answer.
Code:
Dim Time1 As Date
Dim Time2 As Date
Dim TimeDiff As Integer

Time1 = Me.txt22.Value
Time2 = Me.txt23.Value
TimeDiff = (Time2 - Time1) * 86400
Me.lbl1 = Format(TimeDiff / 86400, "hh:mm:ss")

Referring to Micron's BEDMAS comment, and as we all know, multiplication and division are inverse operations.
So why the first procedure returns 00:00:00 and the second one works perfectly is a mystery I would Love to know the answer too.

If anyone has idea's - I'm listening

Thanks Everyone

RT91
 
Upvote 0
Why did you change it to Integer?
Hey Mark...

I changed it to integer because as Double was giving me the answers in seconds down to the nth decimal

Thus 403 seconds would show as 402.82300000000004 seconds.

I hope I answered your question correctly...
 
Upvote 0
I had to go back and see where I got the idea that you were not using parentheses in the equation but I can't find that. I thought you were doing b-a*86400 but you weren't Must have been something I messed up in the immediate widow. My apologies.
 
Upvote 0
The VBA Format function doesn't support the duration format being [h]:mm:ss. Give this a try:

Rich (BB code):
Dim Time1 As Date
Dim Time2 As Date
Dim TimeDiff As Double
Dim sfnTimeDiff As String

Time1 = Me.txt22.Value
Time2 = Me.txt23.Value

TimeDiff = (Time2 - Time1)
sfnTimeDiff = "=Text(" & TimeDiff & ",""[h]:mm:ss"")"

Me.lbl1 = Evaluate(sfnTimeDiff)
Hey Alex,

My apologies for the delayed response.

The code above... Works Perfectly!!

If I understand correctly, your code is processing the TimeDiff as a String
Then, by using the 'Evaluate' function, it turns that String back into a Time Value that can be calculated.
I had to go back and see where I got the idea that you were not using parentheses in the equation but I can't find that. I thought you were doing b-a*86400 but you weren't Must have been something I messed up in the immediate widow. My apologies.
No apology required my Friend...You are far too helpful to be apologizing... :)

When I have time I'm going to evaluate my code (the one that works) in the Immediate Window and see if I can understand why it works?? The Multiplying then Dividing has me shaking my head.
 
Upvote 0

Forum statistics

Threads
1,225,502
Messages
6,185,349
Members
453,287
Latest member
Emeister

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top