We help IT Professionals succeed at work.

# How can I do an If statement using date compare

on
775 Views
<%
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

## View Solution Only

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.

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

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)

Commented:
Many thanks for this Kevp75,  works well.

Commented:
Unlock the solution to this question.