|
-
Aug 3rd, 2006, 08:45 AM
#1
Thread Starter
Hyperactive Member
Time Diff
Hello, I have two times that I would like to find the Hours, Minutes, Seconds between. I am using the following code:
VB Code:
Private Function TimeDiff(strTimeIn As String, strTimeOut As String)
Dim hour As Long, min As Long, sec As Long
sec = DateDiff("s", strTimeIn, strTimeOut)
hour = Int(sec / 3600)
sec = sec - (hour * 3600)
min = Int(sec / 60)
sec = sec - (min * 60)
TimeDiff = Format(hour, "00") & ":" & Format(min, "00") & ":" & Format(sec, "00")
End Function
It works if say I have a TimeIn of 8:00:00AM and a TimeOut of 12:00:00 PM...but if I have a TimeIn of say 01:00:00 PM and a TimeOut of 08:00:00AM (Time diff should be 19 hours)...I get a timediff of -5:00:00. It is going 'back' in time instead of forward. How can I fix this?
Code:
If LostAngel.Tag = "Programming" then
LostAngel.Caption = "Awake"
Else
LostAngel.Caption = "Dreaming of Code"
End If
-
Aug 3rd, 2006, 08:53 AM
#2
Hyperactive Member
Re: Time Diff
Hi LostAngel;
Are you using the full date or just the hh:mm:ss portion?
Your second example won't work without complete dates and times.
Hope this puts you on the right track.
-
Aug 3rd, 2006, 09:08 AM
#3
Re: Time Diff
Try this:
VB Code:
Private Function TimeDiff(strTimeIn As String, strTimeOut As String)
Dim hour As Long, min As Long, sec As Long
Dim time1 As Date, time2 As Date
time1 = CDate(strTimeIn)
time2 = CDate(strTimeOut)
sec = DateDiff("s", time1, time2)
Do While sec < 0 Then
time2 = time2 + 1 'add 1 day
sec = DateDiff("s", time1, time2)
Loop
hour = Int(sec / 3600)
sec = sec - (hour * 3600)
min = Int(sec / 60)
sec = sec - (min * 60)
TimeDiff = Format(hour, "00") & ":" & Format(min, "00") & ":" & Format(sec, "00")
End Function
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
|