Solved

Date comparison

Posted on 2004-10-23
212 Views
Last Modified: 2008-02-01
Hi,

How can I compare if one date is greater than or less than the other using vbscript.  I want use the script in an asp page.

could anyone help me pls.

ayha
0
Question by:ayha1999
    6 Comments
     
    LVL 95

    Expert Comment

    by:Lee W, MVP
    If "mm/dd/yyyy" > "mm/dd/yyyy" Then
        'do stuff
    end if
    0
     
    LVL 95

    Expert Comment

    by:Lee W, MVP
    Or:

    if DateDiff("s", "date1", "date2") > 0 then
        'do stuff
    end if


    DateDiff calculates the difference between two dates.  see http://www.w3schools.com/vbscript/func_datediff.asp
    0
     
    LVL 23

    Expert Comment

    by:apresto
    If you are pulling a date from a table and weant to compare it to the current date:

    If rs("mydate") > date() Then                    '(you can use <, >, >=, <= and <>)
        ...
    End If

    If you want to compare two dates from the same recordset:

    If rs("mydate1") > rs("mydate2") Then
    ...
    End If

    "date()" might be just "date"
    0
     
    LVL 15

    Expert Comment

    by:Thogek
    Just a note:

    > If "mm/dd/yyyy" > "mm/dd/yyyy" Then

    This code will attempt to compare the two strings, which doesn't seem quite what you wanted.  OTOH,

        If #mm/dd/yyyy# > #mm/dd/yyyy# Then

    or

        If CDate("mm/dd/yyyy") > CDate("mm/dd/yyyy") Then

    might give you what you wanted there -- if the dates you are trying to compare are literal (hardcoded) dates in the form of mm/dd/yyyy.

    If the dates you are comparing are date-type values stored within variables (whether acquired from a database or otherwise), that has already been well covered by others above.  :-)
    0
     
    LVL 7

    Author Comment

    by:ayha1999
    @ Thogek,

    I am retrieving a variable called txtDate from users which I want to check again a datefield called rst("date1") from db. could u pls post the correct syntax to compare that the txtdate is less than the other?

    ayha
    0
     
    LVL 15

    Accepted Solution

    by:
    What datatype is txtDate?
    What datatype is rst("date1")?

    From the name, I'm guessing that txtDate is a string that contains date-formatted text.  (Something like "mm/dd/yyyy".)

    And you called rst("date1") a datefield -- does this mean that it is a date/time value?

    If so, something like:

        If CDate(txtDate) > rst("date1") Then
            ' txtDate is after rst("date1")
        End If

    (which is very similar to what apresto suggested above) might lead you toward what you're looking for.

    Description of the VBScript CDate function: http://msdn.microsoft.com/library/en-us/script56/html/vsfctcdate.asp
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now