We help IT Professionals succeed at work.

How can I do an If statement using date compare

BenthamLtd
BenthamLtd asked
on
775 Views
Last Modified: 2012-08-13
<%
Dim CrDate
Dim CrD

CrDate = rsRightNav("cCreatedDate")

If CrDate = now() Then 'If records date is within 7days of now then do this.

Else

End If
%>
Comment
Watch Question

Commented:
CrDate = cdate(rsRightNav("cCreatedDate"))

this will ensure it is converted to a date, which is needed.

or what you could do is do a string compare....


If cstr(CrDate) = cstr(now()) Then 'If records date is within 7days of now then do this.



now, if all you need to do is compare the dates, and not the timestamp (which is what NOW() produces) do something like this:

If formatdatetime(CrDate,2) = formatdatetime(now(),2) Then 'If records date is within 7days of now then do this.


but I see from your note, you want within seven days of now......

If formatdatetime(CrDate,2) <= dateadd("d",-7,now()) Then 'If records date is within 7days of now then do this.

Author

Commented:
I have implemented the following code:

But for dates within the last 7 days, it doesnt flag as True
      <%
      Dim CrDate
      Dim CrD
      CrD = False

      CrDate = cdate(rsRightNav("cCreatedDate"))

      If formatdatetime(CrDate,2) <= dateadd("d",-7,now()) Then 'If records date is within 7days of now then do this.

      CrD = True
      Else
      End If

%>

Commented:
alright.  try this:

      <%
      Dim CrDate
      Dim CrD
      CrD = False

      CrDate = cdate(rsRightNav("cCreatedDate"))
      response.write("date to compare:"&formatdatetime(CrDate,2) & " | now:"&formatdatetime(now,2))
      If formatdatetime(CrDate,2) <= dateadd("d",-7,now()) Then 'If records date is within 7days of now then do this.

      CrD = True
      Else
      End If

%>

and have a look at what the two dates are

Author

Commented:
date to compare:02/10/2007 | now:03/10/2007

So the if statement should be true, yes?
But it defaults to False.

If 02/10/2007 <= 03/10/2007 then
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Many thanks for this Kevp75,  works well.

Commented:
anytime.  Thanks for the grade
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.