Comparing Dates-VBForums
Results 1 to 6 of 6

Thread: Comparing Dates

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    8,529

    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
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved

    DISCLAIMER

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

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

    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
    6,995

    Re: Comparing Dates

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

  4. #4
    Fanatic Member
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    958

    Re: Comparing Dates

    Or you can force-change your dates to long or double and then compare them like any numericals
    For health reasons i try to avoid reading unformatted Code

  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
    6,995

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.