• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

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
0
ayha1999
Asked:
ayha1999
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
If "mm/dd/yyyy" > "mm/dd/yyyy" Then
    'do stuff
end if
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
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
 
aprestoCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ThogekCommented:
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
 
ayha1999Author Commented:
@ 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
 
ThogekCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now