Link to home
Start Free TrialLog in
Avatar of shvanwijlen
shvanwijlen

asked on

compating 2 time variables

Hello,

I have 2 string variables FromTime and ToTime. They are in the format 2:41:36 PM
How can I compare them, like "if FromTime < ToTime then...something...
In Oracle PL/SQL I would convert these strings to a numeric format like 144136 but I don't know how to do that in VB

Thanks a lot!
Simon
Avatar of dancebert
dancebert

CDate( FromTime ) BooleanOperator CDate( ToTime )
Avatar of Richie_Simonetti
you already have your solution!

"...like "if FromTime < ToTime then...something..."

Private Sub Form_Load()
Dim fromtime As Date, totime As Date
fromtime = #9:10:00 AM#
totime = #11:00:00 AM#
If fromtime < totime Then
    MsgBox fromtime & " is less than " & totime
Else
    MsgBox totime & " is less than " & fromtime
End If

End Sub
you can optionally use the DateDiff function for seconds and test on positive or negative.

    Dim zStartTime As Date
    Dim zStopTime As Date
   
    zStartTime = Now()
    zStopTime = DateAdd("s", 15, zStartTime)  'force a 15 second difference
   
    MsgBox DateDiff("s", zStartTime, zStopTime)  'this should say 15, if you reverse Start and Stop, you will get -15

Dim TimeValNow as Long
Dim TimeValSomeOther as Long

TimeValNow = Val(Format(now(), "HHMMSS"))
TimeValSomeOther = Val(Format(Format(ToTime, "Long Time"), "HHMMSS")

Now you can compare TimeValNow vs TimeValSomeOther.

But you could always do comparison similar to

if (Val(Format(Format(FromTime, "Long Time"), "HHMMSS")
 < Val(Format(Format(ToTime, "Long Time"), "HHMMSS"))
    '...
End if
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shvanwijlen

ASKER

Everybody:thanks! the CDate worked best for me, and I just compared the 2 date variables to get wht I needed. Thansk!
"Everybody:thanks! the CDate worked best for me....."

Then, why did you use less than "A" grade for the answer?
>"Everybody:thanks! the CDate worked best for me....."

Then, why didn't you accept the first comment that described CDate and how to use it?