I’ve got a list of times in seconds. Now I know I can just device it by 60 to convert it to Minutes and the Decimal version of seconds. But I’d rather be able to display the time in Minutes:Seconds. Any suggestions?
Duane
MS Access MVP
[green]Ask a great question, get a great answer.[/green]
[red]Ask a vague question, get a vague answer.[/red]
[green]Find out how to get great answers faq219-2884.[/green]
[blue]Ask me about my grandson, get a grand answer.[/blue]
The following will work, but keep an eye on your data types. The same approach can be used if a further conversion to hours is needed. I can't help but think there is an existing VBA function for this, but never found it the last time I looked.
Dim iSec As Integer
Dim iMin As Integer
Dim iM As Integer
Dim strT As String
iSec = YourValueInSeconds
iMin = Round(iSec / 60)
iM = iSec - (iMin * 60)
strT = CStr(iMin) & ":" & CStr(iM)
Here is a way to achieve your result, using integer division and modulus arithmetic. I think these are useful, but I don't often see them used. Start with e.g. 123 seconds. Try this in the Immediate window, to see the results:
The first line is 'normal' division.
The second is integer division - the result is rounded down to show the integer part of the answer
The third is the remainder when (in this case) 123 is divided by 60.
You can use this to convert an integer number of seconds, to minutes and seconds. Here is a sample procedure which you can paste into a module, to test the idea:
Code:
Sub TimeFormattingTest()
Dim intTotalSeconds As Integer
Dim intSeconds As Integer
Dim intMinutes As Integer
Dim strMyTime As String
intTotalSeconds = 123
intSeconds = intTotalSeconds Mod 60
intMinutes = intTotalSeconds \ 60
strMyTime = Format$(intMinutes, "00") & ":" & Format$(intSeconds, "00")
MsgBox strMyTime
End Sub
In my example, strMyTime would contain the value 02:03
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.