?
Solved

compating 2 time variables

Posted on 2003-03-18
8
Medium Priority
?
151 Views
Last Modified: 2010-05-01
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
Comment
Question by:shvanwijlen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 9

Expert Comment

by:dancebert
ID: 8160388
CDate( FromTime ) BooleanOperator CDate( ToTime )
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8160665
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
 
LVL 3

Expert Comment

by:Shaka913
ID: 8161369
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:supunr
ID: 8163078
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 150 total points
ID: 8164677
>> 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
 

Author Comment

by:shvanwijlen
ID: 8164954
Everybody:thanks! the CDate worked best for me, and I just compared the 2 date variables to get wht I needed. Thansk!
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 8166223
"Everybody:thanks! the CDate worked best for me....."

Then, why did you use less than "A" grade for the answer?
0
 
LVL 9

Expert Comment

by:dancebert
ID: 8166814
>"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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question