# Converting Seconds to Hours, Minutes, Seconds

Discussion in 'Access Forms Coding' started by JohnC, Jun 13, 2013.

1. ### JohnCGuest

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

JohnC, Jun 13, 2013

2. ### Rob ParkerGuest

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" <> wrote in message
news:23e22\$51b945ac\$40971c7d\$...
> 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
>
>
>
>
>

Rob Parker, Jun 14, 2013

3. ### JohnCGuest

"JohnC" <> wrote in message
news:23e22\$51b945ac\$40971c7d\$...
> 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

JohnC, Jun 14, 2013
4. ### JohnCGuest

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" <> wrote in message
news:kpdldm\$e3t\$...
> 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" <> wrote in message
> news:23e22\$51b945ac\$40971c7d\$...
>> 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
>>
>>
>>
>>
>>

>

JohnC, Jun 14, 2013