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!

Evaluate Time

Status
Not open for further replies.

Denz

Technical User
Oct 29, 2001
33
GB
Hi Guys,

i'm trying to write a piece of code which simply works out if one time is longer that another. Say for example a one variable is 15 minutes ("00:15:00"), and another is 16 mins then i would like to be able to workout that the second is longer and is 1 minute longer.

I've tried :
Code:
If dtTime > "00:15:00" Then
MsgBox "Longer than 15 mins"
Else
MsgBox "Shorter than 15"
End If
and it ALWAYS says it's shorter.

Any Ideas?

Thanks

David
 
What is the format of dTtime? The two formats have to be equal or else you will not get a valid result.
Also, are you trying to find elapsed time?
How much resolution do you require? (i.e. down to the second, 1/10 of a second, etc.)

I think you are having a formatting problem. If you only require the resolution to be as small as a minute, then simply do...

DtTime = Minute(Dttime)

I think this will work for you.

 
Oh, ok. I see now.

Here is an elapsed time piece of code. For instance, if you have two buttons on a form and you want to tell how much time elapsed between the two buttons, then you would do something like this...

Public But1Time as static 'put these in the "general" part of the code
Public But2Time as double


Under the first button put this code...

But1Time = timer()

Under the second button put this code...
Dim ElapTime as double
But2Time = Timer()
ElapTime = But2Time - But1Time
Msgbox ElapTime,vbinformaiton,"This is the Elapsed Time..."

Try it and see!

LF

 
I messed up...
it should be Static But1Time as Double. AND you can't put it in the General section...

sorry, the concept is there, maybe somebody else can help

LF

 
Ok I got it...


create a module and put this code inside it

Function TimeIt(Num)
Static but1time As Double
Static but2time As Double
Dim elaptime As Double

If Num = 1 Then
but1time = Timer()
Else
but2time = Timer()
elaptime = but2time - but1time
TimeIt = CStr(elaptime)
End If
End Function

OK, now to call this function all you do is under the first button put...

Timeit(1)

ok?

now, under the second button, put

msgbox timeit(2)

this works!!!


LF
 
Both of the times are formatted in "HH:mm:ss"

i need to *idealy* make it work out to the second.

When i minus one from the other i can see it has worked out the correct time, (i.e. 00:16:00) however when i try to get it to evaluate the result to see if it's longer than 15 mins, it's always shorter!
 
use the datediff function

Datediff("s",00:15:00","00:16:00")
this will give you 60

Datediff("s", "00:16:00","00:15:00")
this will give you -60
 
I used this function with great success in a timeclock program I wrote. which kept track of 10s of 1000's of times

Public Function DiffTime(TimeIN, TimeOUT)
On Error GoTo Errhand
If IsNull(TimeIN) Then
Msg = "You did not punch IN yet" & Chr$(10)
MsgBox Msg & "Please do so"
GoTo ExitMe:
End If
'debug.print "Time IN "; TimeIN, "TimeOut "; TimeOUT
Hr1 = DatePart("h", TimeIN)
Mn1 = DatePart("n", TimeIN)
SS1 = DatePart("s", TimeIN)
Hr2 = DatePart("h", TimeOUT)
Mn2 = DatePart("n", TimeOUT)
SS2 = DatePart("s", TimeOUT)

DiffTime2 = TimeSerial(Hr2 - Hr1, Mn2 - Mn1, SS2 - SS1)
'debug.print "DiffTime2 "; DiffTime2
If DiffTime2 < "1:00 AM" Then
'debug.print DiffTime2
Mint = DatePart("n", DiffTime2)
If Len(Mint) = 1 Then Mint = "0" & Mint
DiffTime2 = "00:" & Mint & ":" & DatePart("s", DiffTime2)
DiffTime = DiffTime2
Else
DiffTime = Format(DiffTime2, "HH:MM:SS")
End If
'debug.print "DiffTime > "; DiffTime

ExitMe:
Exit Function

Errhand:
Select Case Err.Number
Case 94
' Invalid use of null
MsgBox "You did not punch in"
Case Else
MsgBox Err.Number & " " & Err.Description
Resume ExitMe
End Select
End Function



DougP, MCP, A+
 
Denz,

The function I wrote for you will give you the elapsed time in Millionths of a second if you wished it to. To get to a resolution of seconds, then simply put this code under the part where you call the function for the second time...

msgbox format(timeit(2),"00:00:00")


Now all you have to do is have your program call this function once to initiate the timing event, and then again to actually return the amount of time which has elapsed. The function I wrote for you has got to be the easiest, and shortest, way to tell elapsed time. It is easy, and sometimes funner, to over engineer things and try to reinvent the wheel, however, in the end the only thing that makes a difference is that the program works! I hope my code is some help to you.

LF
 
Do a Datediff in seconds then check if that is positive or not

delay = datediff("s",time1,time2)
if delay > 0 then
msgbox "longer by " & delay & " seconds"
else msgbox "shorter by " & delay & " seconds
end if


________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top