Results 1 to 9 of 9

Thread: Compare Dates [resolved]

  1. #1

    Thread Starter
    Fanatic Member demotivater's Avatar
    Join Date
    Jun 2002
    Location
    is everything
    Posts
    627

    Compare Dates [resolved]

    What's the best way to check if one date is greater than another? I have two textboxes with dates in them, want to make sure one of the dates is not greater than the other. The below works sometimes, but I just ran into a case where it isn't working.

    VB Code:
    1. If txtBidDate.text <> "" Then
    2.                         If txtReviewDate.text > txtBidDate.text Then
    3.                             MsgBox "The review date cannot be past the bid date", vbCritical, "Error"
    4.                             txtReviewDate.text = ""
    5.                             cmdReviewDate.SetFocus
    6.                             Exit Sub
    7.                         End If
    8.                     End If

    reviewdate = 6/10/04
    biddate = 6/9/04

    Is it because I need to format them both to = mm/dd/yyyy so 6/9/04 would become 6/09/04, or is there a better way?

    Thanks in advance
    Last edited by demotivater; Jun 4th, 2004 at 02:26 PM.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Since you are comparing string variables and not dates, make sure the strings are the exact same length and format.

    by the way mm/dd/yyyy is 06/04/2004

    Another option is to convert the text values to date variables and use the DateDiff function.

  3. #3
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951
    I would do this:

    if cdate(txtdate1.text) > cdate(txtdate2.text) then...

  4. #4
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951
    txtdate1.text = format(cdate(txtdate1.text),"mm/dd/yyy")

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    One more thing. The dates should always be in year-month-day format as in "2004/06/04". Otherwise, the following would test true.

    If "06/04/2000" > "06/03/2004" Then ....

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Originally posted by ccoder
    One more thing. The dates should always be in year-month-day format as in "2004/06/04". Otherwise, the following would test true.

    If "06/04/2000" > "06/03/2004" Then ....
    Only because they are being treated as strings. If converted to Date data type, then the proper comparison will take place and return the right result.

    TG
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654
    You might want to use DateSerial to get the dates correct. Of course you have to parse the date strings by yourself, but atleast that way they go right no matter what.

    VB Code:
    1. Dim Date1() As String, Date2() As String
    2. Dim rDate As Date, bDate As Date
    3.  
    4. 'parse data
    5. Date1 = Split(txtReviewDate.Text, "/")
    6. Date2 = Split(txtBidDate.Text, "/")
    7. 'check for incorrect number of elements
    8. If UBound(Date1) <> 2 Then Exit Sub
    9. If UBound(Date2) <> 2 Then Exit Sub
    10. 'make actual dates (year, month, day)
    11. rDate = DateSerial(Date1(2), Date1(1), Date1(0))
    12. bDate = DateSerial(Date2(2), Date2(1), Date2(0))
    13. If rDate > bDate Then 'the date is bigger
    14.     'do stuff
    15. Else 'it is not
    16.     'do stuff
    17. End If

  8. #8

    Thread Starter
    Fanatic Member demotivater's Avatar
    Join Date
    Jun 2002
    Location
    is everything
    Posts
    627
    Thanks for the suggestions guys.


  9. #9
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    Originally posted by techgnome
    Only because they are being treated as strings. If converted to Date data type, then the proper comparison will take place and return the right result.

    TG
    Agreed! I had actually responded to the post by brucevde, but failed to quote the post.

    FWIW, in all of my years of programming, I have always worked with dates as a "string" of numbers only. Beginning with COBOL and Fortran back in the early 70's and later with PL/1 and C (and a few obscure language variations like MOBOL and SCOBOL). Dates were always stored in [yy]yymmdd format. Back then memory & storage costs were tremendous compared to today. It was too costly to store the "/". The big Y2K flap was a result of storing dates with just the 2 low order digits - another cost saving measure. No one ever dreamed that it would come back to bite them.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width