Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

seconds to hours, minutes and seconds

If I have seconds, is there function which returns me hours minutes and seconds?

example:
seconds=3602
I need a function/functions which returns me 1 hour 0 minutes 2 seconds (hope I counted it right).

Thanks in advance!
0
majala
Asked:
majala
  • 2
1 Solution
 
RuchiCommented:
I posted this code to this similiar code.

'Declarations:

'(None)

'Code:

Public Function TimeString(Seconds As Long, Optional Verbose _
As Boolean = False) As String

'if verbose = false, returns
'something like
'02:22.08
'if true, returns
'2 hours, 22 minutes, and 8 seconds

Dim lHrs As Long
Dim lMinutes As Long
Dim lSeconds As Long

lSeconds = Seconds

lHrs = Int(lSeconds / 3600)
lMinutes = (Int(lSeconds / 60)) - (lHrs * 60)
lSeconds = Int(lSeconds Mod 60)

Dim sAns As String


If lSeconds = 60 Then
  lMinutes = lMinutes + 1
  lSeconds = 0
End If

If lMinutes = 60 Then
  lMinutes = 0
  lHrs = lHrs + 1
End If

sAns = Format(CStr(lHrs), "#####0") & ":" & _
Format(CStr(lMinutes), "00") & ":" & _
Format(CStr(lSeconds), "00")

If Verbose Then sAns = TimeStringtoEnglish(sAns)
TimeString = sAns

End Function

Private Function TimeStringtoEnglish(sTimeString As String) _
As String

Dim sAns As String
Dim sHour, sMin As String, sSec As String
Dim iTemp As Integer, sTemp As String
Dim iPos As Integer
iPos = InStr(sTimeString, ":") - 1

sHour = Left$(sTimeString, iPos)
If CLng(sHour) <> 0 Then
  sAns = CLng(sHour) & " hour"
  If CLng(sHour) > 1 Then sAns = sAns & "s"
  sAns = sAns & ", " 
End If

sMin = Mid$(sTimeString, iPos + 2, 2)

iTemp = sMin

If sMin = "00" Then
 sAns = IIf(Len(sAns), sAns & "0 minutes, and ", "")
Else
 sTemp = IIf(iTemp = 1, " minute", " minutes")
 sTemp = IIf(Len(sAns), sTemp & ", and ", sTemp & " and ")
 sAns = sAns & Format$(iTemp, "##") & sTemp
End If

iTemp = Val(Right$(sTimeString, 2))
sSec = Format$(iTemp, "#0")
sAns = sAns & sSec & " second"
If iTemp <> 1 Then sAns = sAns & "s"

TimeStringtoEnglish = sAns

End Function


Private Sub Form_Load()
    MsgBox TimeString(3602)
End Sub
0
 
RuchiCommented:
I meant that I posted this code to this similiar question. (not code)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now