Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code query re: calculating times 1

Status
Not open for further replies.

id1912

Technical User
Jan 22, 2003
1
GB
I am using the following code so that I can add add times
together so that they don't reset themselves at 24:00, for
example 17:00 + 15:00 = 32:00 instead of 08:00.

Public Function FormatTime(dtmTime)
Dim Hours As Long
Dim Minutes As Long

If IsNull(dtmTime) Then Exit Function

Hours = Int(dtmTime * 24)

Minutes = (dtmTime * 24 - Hours) * 60

If Minutes = 60 Then
Hours = Hours + 1
Minutes = 0
End If

FormatTime = Hours & ":" & Format(Minutes, "00")

End Function

Unfortunately I now require to show my reports with seconds in my times - hh:mm:ss.

Could anyone assist in helping me add seconds to this
code? or would I require totally new code??? or at the very least point me in the right direction.

Thanks
 
Public Function FormatTime(dtmTime)
Dim Hours As Long
Dim Minutes As Long
dim seconds as long

If IsNull(dtmTime) Then Exit Function

Hours = Int(dtmTime * 24)

Minutes = (dtmTime * 24 - Hours) * 60

seconds = ((dtmTim * 24 - hours) * 60) - minutes
if seconds = 60 then
minutes = minutes+1
seconds = 0
end if


If Minutes = 60 Then
Hours = Hours + 1
Minutes = 0
End If

FormatTime = Hours & ":" & Format(Minutes, "00") & ":" & format(seconds, "00")

End Function

try that.. all i did was expand on your previous logic. Cruz'n and Booz'n always.
This post shows what little I do at work.
 
correction:

replace Minutes = (dtmTime * 24 - Hours) * 60
with
Minutes = int((dtmTime * 24 - Hours) * 60)

(based on your logic)




Cruz'n and Booz'n always.
This post shows what little I do at work.
 
hmmmmmmmmmmmmmmmmmm, mayhappaby there be a different skin on yo olde cat/

review the following, to see if it is useful for you

Code:
dtmTime = 1.3755

MyHrs = Val(Trim(Format(dtmTime, "h")))
MyHrs = MyHrs + (Int(dtmTime) * 24)

MyTime = Str(MyHrs) & Format(dtmTime, ":mm:ss")
? MyTime
 33:12:43
[/code

It is (was?) obviously just scribbled into the debug (immediate -for purist) window, and 'dragged out' - just to illustrate the individual steps, but any 'good' programmer could easily set the steps into a simplistic procedure and / or consolidate severl or more of the steps ...


 MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
I'm unclear as to how your parameter is getting fed to your function.
The function is taking Access' native date storage format and
converting it to an dd:hh:mm format, e.g.
? formattime(1.543678)
37:03

Apparently there's a calling function (which you didn't show)
that adds the times together.

Anyway, here are two functions that will allow you to add two times,
convert the result to seconds and then return the result in dd:hh:mm:ss
format.

The debug.print statements can be commented-out.
Code:
Function TimeAdd(starttime As Date, endtime As Date) As Variant
'*******************************************
'Name:      TimeAdd (Function)
'Purpose:   Returns sum of two times(in datetype data format)
'           as # of seconds
'Calls:     Function FmtDate
'Inputs:    ? timeadd(#12:10:05#, #13:10:10#)
'Output:    01:01:20:15 i.e. dd:hh:mm:ss
'*******************************************

Dim timehold As Date
Dim x As Double, y As Double, z As Double
'sum of times expressed in Access date format
'e.g.
timehold = starttime + endtime
Debug.Print timehold
Debug.Print CDbl(timehold)
Debug.Print Int(CDbl(timehold))

x = 86400 * Int(CDbl(timehold))
Debug.Print x
y = 86400 * (CDbl(timehold) - Int(CDbl(timehold)))
Debug.Print y
z = x + y
Debug.Print z
TimeAdd = FmtDate(z)
End Function

Function FmtDate(ByRef pdte As Double) As String
'*******************************************
'Name:      FmtDate (Function)
'Purpose:   format # of seconds as dd:hh:mm:ss
'Inputs:    ? FmtDate(95123)
'Output:    01:02:25:23
'*******************************************

'pDte = total seconds
Dim timehold As Double
Dim xdays, xhrs, xmins, xsecs, x
x = "00"  'Formatting template
timehold = pdte
xdays = timehold \ 86400
xhrs = (timehold \ 3600) - (xdays * 24)
xmins = (timehold \ 60) Mod 60
xsecs = timehold Mod 60
FmtDate = Format(xdays, x) & ":" & Format(xhrs, x) & ":" & Format(xmins, x) & ":" & Format(xsecs, x)
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top