Converting Seconds to Hours, Minutes, Seconds

J

JohnC

So I'm using a procedure to take separate unbound controls for hours,
minutes, and seconds and storing the computed seconds in a table. This is
what I'm using:

Me.txtRaceTimeSeconds = 3600& * Nz(Me.txthours, 0&) + (60& *
Nz(Me.txtMinutes, 0&) + Nz(Me.txtSeconds, 0&))

This works fine and I can enter hundreds of a second such as 23.23 in
txtSeconds and all is good.

Now when I try to display Hours, Minutes and seconds from the stored Seconds
field I start having problems. Using:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" &
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Me.[txtRaceTimeSeconds] Mod 60, "00")

will display everyting properly except if hundreths are entered in seconds,
i.e. 23.23 the hundreths won't display.

I've tried just about everything to get the numbers to the right of the
decimal including:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" &
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Me.[txtRaceTimeSeconds] Mod 60, "00") &
Format$(Mid(Me.txtRaceTimeSeconds, Len(Int(Me.txtRaceTimeSeconds * 100)) -
1), "#.00")

but I get an error if seconds are zero using that. And I can't get properly
formated HH.MM.SS.ss if there are no hundreths. It drops the .ss
 
R

Rob Parker

The problem is that the Mod operator rounds floating point numbers to
integers. Therefore, you need to add the decimal part to your expression,
after the various Mod operations. This nearly works:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" & _
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Me.[txtRaceTimeSeconds] Mod 60, "00") & _
Format([txtRaceTimeSeconds]-Int([txtRaceTimeSeconds]),".00")

The problem is that it rounds up the seconds in the final Mod operation.
You can get around this by applying the Int function before that Mod
operation, so the following works correctly:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" & _
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Int(Me.[txtRaceTimeSeconds]) Mod 60, "00") & _
Format([txtRaceTimeSeconds]-Int([txtRaceTimeSeconds]),".00")


HTH,

Rob
 
J

JohnC

JohnC said:
So I'm using a procedure to take separate unbound controls for hours,
minutes, and seconds and storing the computed seconds in a table. This is
what I'm using:

Me.txtRaceTimeSeconds = 3600& * Nz(Me.txthours, 0&) + (60& *
Nz(Me.txtMinutes, 0&) + Nz(Me.txtSeconds, 0&))

This works fine and I can enter hundreds of a second such as 23.23 in
txtSeconds and all is good.

Now when I try to display Hours, Minutes and seconds from the stored
Seconds field I start having problems. Using:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" &
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Me.[txtRaceTimeSeconds] Mod 60, "00")

will display everyting properly except if hundreths are entered in
seconds, i.e. 23.23 the hundreths won't display.

I've tried just about everything to get the numbers to the right of the
decimal including:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" &
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Me.[txtRaceTimeSeconds] Mod 60, "00") &
Format$(Mid(Me.txtRaceTimeSeconds, Len(Int(Me.txtRaceTimeSeconds * 100)) -
1), "#.00")

but I get an error if seconds are zero using that. And I can't get
properly formated HH.MM.SS.ss if there are no hundreths. It drops the .ss

Got it working!
Public Sub ConvSecondstoHHMMSS()

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & _
":" & Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & _
":" & Format$(Me.[txtRaceTimeSeconds] - (60 * (Me.[txtRaceTimeSeconds] \
60)), "00.00")

End Sub
 
J

JohnC

Thanks Rob,
Yours works.

This also works:
Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & _
":" & Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & _
":" & Format$(Me.[txtRaceTimeSeconds] - (60 * (Me.[txtRaceTimeSeconds] \
60)), "00.00")


Rob Parker said:
The problem is that the Mod operator rounds floating point numbers to
integers. Therefore, you need to add the decimal part to your expression,
after the various Mod operations. This nearly works:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" & _
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Me.[txtRaceTimeSeconds] Mod 60, "00") & _
Format([txtRaceTimeSeconds]-Int([txtRaceTimeSeconds]),".00")

The problem is that it rounds up the seconds in the final Mod operation.
You can get around this by applying the Int function before that Mod
operation, so the following works correctly:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" & _
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Int(Me.[txtRaceTimeSeconds]) Mod 60, "00") & _
Format([txtRaceTimeSeconds]-Int([txtRaceTimeSeconds]),".00")


HTH,

Rob

JohnC said:
So I'm using a procedure to take separate unbound controls for hours,
minutes, and seconds and storing the computed seconds in a table. This
is what I'm using:

Me.txtRaceTimeSeconds = 3600& * Nz(Me.txthours, 0&) + (60& *
Nz(Me.txtMinutes, 0&) + Nz(Me.txtSeconds, 0&))

This works fine and I can enter hundreds of a second such as 23.23 in
txtSeconds and all is good.

Now when I try to display Hours, Minutes and seconds from the stored
Seconds field I start having problems. Using:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" &
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Me.[txtRaceTimeSeconds] Mod 60, "00")

will display everyting properly except if hundreths are entered in
seconds, i.e. 23.23 the hundreths won't display.

I've tried just about everything to get the numbers to the right of the
decimal including:

Me.txtHoursMinutesSeconds = Me.txtRaceTimeSeconds \ 3600 & ":" &
Format$((Me.[txtRaceTimeSeconds] \ 60) Mod 60, "00") & ":" _
& Format$(Me.[txtRaceTimeSeconds] Mod 60, "00") &
Format$(Mid(Me.txtRaceTimeSeconds, Len(Int(Me.txtRaceTimeSeconds *
100)) - 1), "#.00")

but I get an error if seconds are zero using that. And I can't get
properly formated HH.MM.SS.ss if there are no hundreths. It drops the
.ss
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top