Solved

seconds to hours, minutes and seconds

Posted on 2000-03-10
2
209 Views
Last Modified: 2010-05-02
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
Comment
Question by:majala
  • 2
2 Comments
 
LVL 9

Accepted Solution

by:
Ruchi earned 30 total points
ID: 2603636
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
 
LVL 9

Expert Comment

by:Ruchi
ID: 2603641
I meant that I posted this code to this similiar question. (not code)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now