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
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
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 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"
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. :-)
> 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. :-)
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
'do stuff
end if