Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

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
0
shvanwijlen
Asked:
shvanwijlen
1 Solution
 
dancebertCommented:
CDate( FromTime ) BooleanOperator CDate( ToTime )
0
 
Richie_SimonettiIT OperationsCommented:
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
0
 
Shaka913Commented:
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

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
supunrCommented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> I have 2 string variables FromTime and ToTime

Dim ft As Date
Dim tt As Date
Dim diff As Long

ft = CDate (FromTime)
tt = CDate (ToTime)

diff = DateDiff ( "h", FromTime, ToTime ) ' gives difference between them in hours

diff = DateDiff ( "n", FromTime, ToTime ) ' gives difference between them in minutes - note that it is "n", not "m"
diff = DateDiff ( "s", FromTime, ToTime ) ' gives difference between them in seconds


Hope that helps!

Mayank.

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

Then, why did you use less than "A" grade for the answer?
0
 
dancebertCommented:
>"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?
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now