|
-
May 30th, 2006, 08:39 PM
#1
Thread Starter
Hyperactive Member
converting seconds
How would I turn a number in seconds into the normal time format of 0:00:00:000 (hours:minutes:seconds:milliseconds)
So, if the number of seconds was 125 then the time would be
0:02:05
Then show it without the erroneous numbers to make
2:05
Another problem is to show the 0 when the seconds is less than ten.
So, instead of 0:3 show 0:03
Sorry if I have confused any of you. thanks.
Prefix has no suffix, but suffix has a prefix.
-
May 30th, 2006, 08:48 PM
#2
Re: converting seconds
something like this?:
VB Code:
Private Function ConvertSeconds(ByVal lSec As Long) As String
ConvertSeconds = Format$(lSec \ 3600, "00") & ":" & _
Format$((lSec Mod 3600) \ 60, "00") & ":" & _
Format$(lSec Mod 60, "00")
End Function
Last edited by bushmobile; May 30th, 2006 at 08:52 PM.
-
May 30th, 2006, 09:03 PM
#3
Re: converting seconds
How about TimeSerial() function:
VB Code:
Private Sub Command1_Click()
Dim iSeconds As Long
iSeconds = 7276
Debug.Print Format(Hour(TimeSerial(0, 0, iSeconds)), "00") & _
":" & _
Format(Minute(TimeSerial(0, 0, iSeconds)), "00") & _
":" & _
Format(Second(TimeSerial(0, 0, iSeconds)), "00")
End Sub
Output should be: 02:01:16
-
May 30th, 2006, 09:07 PM
#4
Re: converting seconds
Probably wouldn't be an issue, but the TimeSerial method is limted to 9hrs, 6mins and 7secs or so.
-
May 30th, 2006, 09:12 PM
#5
Re: converting seconds
Where did you get those numbers from?
The actual limitations are the arguments (hour,minute,second) which are defined as Integers so if you pass any greater number you'll facwe the "overflow" error... But in most cases as you said it "wouldn't be an issue".
-
May 30th, 2006, 09:13 PM
#6
Re: converting seconds
32767 seconds = 9hrs, 6mins and 7secs
-
May 30th, 2006, 09:17 PM
#7
Re: converting seconds
Oh common ... who cares about that? Error is caused by overflowing Integer with Long. Are you kidding me?
-
May 30th, 2006, 09:28 PM
#8
Re: converting seconds
eh?
I know what causes the error.
i didn't mean the TimeSerial function itself was limited to 9hrs, 6mins and 7secs, just that if Troy is going to use the TimeSerial method in the form you suggested then he'd better be aware that it can't evaluate time periods longer than 9hrs, 6mins and 7secs.
-
May 30th, 2006, 10:06 PM
#9
Thread Starter
Hyperactive Member
Re: converting seconds
Calm down a little.
I use this:
VB Code:
Private Sub Command1_Click()
Label1.Caption = ConvertMilliSeconds(7600)
End Sub
Private Function ConvertMilliSeconds(ByVal mSec As Long) As String
ConvertMilliSeconds = Format$(mSec \ 3600000, "00") & ":" & _
Format$((mSec \ 60000), "00") & ":" & _
Format$(mSec \ 1000, "00") & "." & _
Format$(mSec Mod 1000, "000")
End Function
Now, there is one thing that I wanted to comment on. I noticed that with the code above, as is, Label1 displays 00:00:07.600. However, if I were to change the "\" sign for seconds to the "/" sign, it would be 00:00:08.600.
Why is that?
Prefix has no suffix, but suffix has a prefix.
-
May 30th, 2006, 10:29 PM
#10
Re: converting seconds
\ is integer division, so 7600 \ 1000 = 7
/ is normal division, so 7600 / 1000 = 7.6
the format function performs rounding, so you end up with 8 instead of 7.
your function won't work for a couple of reasons (try putting in 77600 for example). Try this:
VB Code:
Private Function ConvertMilliSeconds(ByVal mSec As Long) As String
ConvertMilliSeconds = Format$(mSec \ 3600000, "00") & ":" & _
Format$((mSec Mod 3600000) \ 60000, "00") & ":" & _
Format$((mSec Mod 60000) / 1000, "00.000")
End Function
Last edited by bushmobile; May 30th, 2006 at 11:13 PM.
-
May 30th, 2006, 11:10 PM
#11
Thread Starter
Hyperactive Member
Re: converting seconds
Ok, I rewrote it like so:
VB Code:
Private Function cSeconds(ByVal mSec As Long) As String
mSec = 30000
Select Case mSec
Case Is >= 600, Is < 3600
cSeconds = _
Format$((mSec Mod 3600) \ 60, "00") & ":" & _
Format$(mSec Mod 60, "00")
Case Is < 600
cSeconds = "0:" & _
Format$(mSec Mod 60, "00")
Case Is >= 3600, Is < 36000
cSeconds = _
Format$(mSec \ 3600, "00") & ":" & _
Format$((mSec Mod 3600) \ 60, "00") & ":" & _
Format$(mSec Mod 60, "00")
End Select
End Function
Now there is one problem. With mSec at 30000 it completely ignores the first part of the string (the hours) and it returns "20:00". What is going on? No matter what number I make mSec, it will not show the hours. I tried 3599 which returned "59:59" then 3600 which returned "00:00" then 3601 which returned "00:01"
Why is it not showing the hours?
Prefix has no suffix, but suffix has a prefix.
-
May 30th, 2006, 11:16 PM
#12
Re: converting seconds
well if msec is greater than 600, it'll go into the first case.
i missed off a zero in my code, in case you hadn't noticed (which I've now changed)
-
May 30th, 2006, 11:23 PM
#13
Re: converting seconds
Edit Perhaps do this:
VB Code:
Private Function ConvertMilliSeconds(ByVal mSec As Long) As String
If mSec >= 3600000 Then ConvertMilliSeconds = Format$(mSec \ 3600000, "00") & ":"
If mSec >= 60000 Then ConvertMilliSeconds = ConvertMilliSeconds & Format$((mSec Mod 3600000) \ 60000, "00") & ":"
ConvertMilliSeconds = ConvertMilliSeconds & Format$((mSec Mod 60000) / 1000, "00.000")
End Function
Last edited by bushmobile; May 30th, 2006 at 11:29 PM.
Reason: Replaced code with better version
-
May 30th, 2006, 11:36 PM
#14
Re: converting seconds
Adding up into the chaos!
VB Code:
Public Type HourMinuteSecondMS
Hours As Integer
Mins As Byte
Secs As Byte
MS As Integer
End Type
Public Function GetHMSfromMS(ByVal Milliseconds As Long) As HourMinuteSecondMS
GetHMSfromMS.Hours = CInt(Milliseconds \ 3600000)
GetHMSfromMS.Mins = CByte((Milliseconds Mod 3600000) \ 60000)
GetHMSfromMS.Secs = CByte((Milliseconds Mod 60000) \ 1000)
GetHMSfromMS.MS = CInt(Milliseconds Mod 1000)
End Function
Public Function GetStrFromMS(ByVal Milliseconds As Long, Optional ByVal AlwaysFull As Boolean) As String
Dim lngHours As Long, lngMins As Long, lngSecs As Long, lngMS As Long
Dim blnHours As Long, blnMins As Long
lngHours = Milliseconds \ 3600000
blnHours = (lngHours > 0) Or AlwaysFull
lngMins = (Milliseconds Mod 3600000) \ 60000
blnMins = (lngMins > 0) Or blnHours
lngSecs = (Milliseconds Mod 60000) \ 1000
lngMS = Milliseconds Mod 1000
If blnHours Then
GetHMSfromMS = CStr(lngHours) & ":" & Format$(lngMins, "00") & ":" & _
Format$(lngSecs, "00") & "." & Format$(lngMS, "000")
ElseIf blnMins Then
GetHMSfromMS = Format$(lngMins, "00") & ":" & _
Format$(lngSecs, "00") & "." & Format$(lngMS, "000")
Else
GetHMSfromMS = Format$(lngSecs, "00") & "." & Format$(lngMS, "000")
End If
End Function
These are two different kinds of function. The other returns, at minimum, 00.000, but it automatically adds minutes or hours if necessary, so you end up with 00:00.000 or 0:00:00.000 formatted strings.
GetHMSfromMS instead returns a udt which contains all values splitted up nicely. This is useful if you want to make a custom digital display or something of that kind instead of using a label.
-
May 31st, 2006, 12:11 AM
#15
Thread Starter
Hyperactive Member
Re: converting seconds
 Originally Posted by bushmobile
well if msec is greater than 600, it'll go into the first case.
You are quite right. I didn't even notice it. I put the at the top and it works now.
Thank you all for your help.
Prefix has no suffix, but suffix has a prefix.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|