Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

Date comparison

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
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

If "mm/dd/yyyy" > "mm/dd/yyyy" Then
    'do stuff
end if
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
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"
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.  :-)
Avatar of ayha1999
ayha1999

ASKER

@ 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
ASKER CERTIFIED SOLUTION
Avatar of Thogek
Thogek
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial