Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date comparison

Posted on 2004-10-23
6
Medium Priority
?
214 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
Comment
Question by:ayha1999
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 96

Expert Comment

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

Expert Comment

by:Lee W, MVP
ID: 12389165
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
ID: 12390237
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:Thogek
ID: 12391615
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
ID: 12394252
@ 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:
Thogek earned 120 total points
ID: 12397404
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

609 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