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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating time problem

Status
Not open for further replies.

ckeener

Programmer
Dec 2, 2003
53
US
I am trying to calculate a time and this does not seem to calculate the hour. The table that I get the information from has an end time (ex. 7:10:31 AM) and a duration (in seconds - ex. 48610).

I am attempting to calculate the start-time by subtracting the duration from the end time. The problem seems to be that the information that the function is getting when I try to pass the end time adds a date as well . (Ex. 12/29/1899 7:10:31 AM)

If I hard code just the time in the function call then the function works great. (Checktime("7:10:31 AM", 48610) = 3:10:21 PM) However when I pass the date information straight from the table, it tacks the 12/29/1899 date at the beginning.


Public Function CheckTime(Begin As Date, Duration As Long) As Date
CheckTime = TimeSerial(Hour(Begin - Int(Duration \ 3600)), Minute(Begin), _
(Second(Begin) - (Duration - (Int(Duration \ 3600) * 3600))))
End Function


Is there anything I can do to remedy this problem?

Thanks
 
I get 12/29/1899 5:40:21 PM using

Code:
CheckTime = DateAdd ("s", -48610, #7:10:31 AM# )

Since you are returning a date field there will always be a date component. You can supress it using
Code:
CheckTime = TimeValue ( DateAdd ("s", -48610, #7:10:31 AM# ))
 
The following test worked on my system fine,
Code:
Public Sub test()
Dim d As Double
Dim t As Date
Dim s As String

t = CDate("7:10:31 AM")
d = CDbl(t)
s = "0," + "48610"
d = d - CDbl(s)
MsgBox (CDate(d))
End Sub

returns 04:29:28

note : I'm working on a german system you may need to use "0." instead.


Microsoft said:
Date (Data type)
is a 64 bit / 8 bite reelle variable.
the value left of the decimal is date, the value right of the decimal is time.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top