Results 1 to 6 of 6

Thread: Comparing Dates

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Comparing Dates

    How to compare two dates and determine if MyDate is less than VB Date?

    MyDate = "11/15/2012"

    Vb Date = "11/14/2012"

    If MyDate < Date Then
    '
    ' Do Not Allow
    '
    Else
    '
    ' Allow
    '
    End if


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Comparing Dates

    Dim myDate As Date, vDate As Date
    myDate = "12/15/2012"
    vDate = "11/16/2012"
    If myDate < vDate Then
    MsgBox "Do Not Allow"
    Else
    MsgBox "Allow"
    End If

    OR


    Dim myDate As Date
    myDate = "11/15/2012"
    If myDate < date Then
    MsgBox "Do Not Allow"
    Else
    MsgBox "Allow"
    End If

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Comparing Dates

    You could also use the datediff() function and check the return value

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Comparing Dates

    Or you can force-change your dates to long or double and then compare them like any numericals
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5
    New Member
    Join Date
    Nov 2012
    Posts
    11

    Re: Comparing Dates

    Or you could try:

    Code:
    If Format("14/11/2012", "dd-mm-yy") > Format(Now, "dd-mm-yy") Then
        Debug.Print "Bigger"
    Else
        Debug.Print "Smaller"
    End If
    Datediff:

    Code:
    If DateDiff("d", Now, "14/11/2012") > 0 Then
        Debug.Print "Bigger"
    Else
        Debug.Print "Smaller"
    End If

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Comparing Dates

    Note that the code to compare dates using < > must be done using Date variables.

    The code shown above using format will not work correctly due to the usage of strings instead of dates.
    i.e.

    14/12/2012 < 15/11/2012
    15/11/2012 < 31/12/2000

    12/31/1900 > 1/1/2000

    The only way to make it work using strings is to use the yyyy/mm/dd format

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