Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

How can I do an If statement using date compare

<%
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
%>
0
BenthamLtd
Asked:
BenthamLtd
  • 4
  • 3
1 Solution
 
kevp75Commented:
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.
0
 
BenthamLtdAuthor 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

%>
0
 
kevp75Commented:
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
0
Independent Software Vendors: 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!

 
BenthamLtdAuthor 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
0
 
kevp75Commented:
try this out:
      Dim CrDate
      Dim CrD
      CrD = False

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

      CrD = True
      Else
      End If
0
 
BenthamLtdAuthor Commented:
Many thanks for this Kevp75,  works well.
0
 
kevp75Commented:
anytime.  Thanks for the grade
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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