Solved

Adding and Subtracting Dates

Posted on 2009-05-13
3
244 Views
Last Modified: 2012-05-06
Hi Experts.   I have a bit of code that I'm taking from and old classic ASP/ VB Script page and trying to implement in a VB.NET/ASP.NET web application and I'm getting this error.

Operator '-' is not defined for type 'Date' and type 'Integer'.

The error is pointing to the 'If' statement.

My Code ( In the attached snippet)  works perfectly in Classic ASP/VB Script but .NET doesn't like it.  

All I want to do is add and or subtract days from the date value returned from my database.

What do I need to do in order to make this code function in .NET?
If reader.HasRows Then

                While reader.Read()

                    If ((reader("EventStartDate") - 6) <= StartDate And StartDate <= (reader("EventEndDate") + 6)) _

                    Or ((reader("EventStartDate") - 6) <= EndDate And EndDate <= (reader("EventEndDate") + 6)) _

                    Or (StartDate <= (reader("EventStartDate") - 6) And EndDate >= (reader("EventStartDate") - 6)) Then

                        intQuantityOut += reader("QuantityOrdered")

                    End If

                End While
 

                Return intQuantityOut
 

            Else

                Return 0

            End If

Open in new window

0
Comment
Question by:cdemott33
  • 2
3 Comments
 
LVL 10

Expert Comment

by:MaxOvrdrv2
Comment Utility
you need to use DateAdd() function...

DateAdd(interval,number,date)

Parameter Description
interval Required. The interval you want to add
Can take the following values:

yyyy - Year
q - Quarter
m - Month
y - Day of year
d - Day
w - Weekday
ww - Week of year
h - Hour
n - Minute
s - Second
 
number Required. The number of interval you want to add. Can either be positive, for dates in the future, or negative, for dates in the past

date Required. Variant or literal representing the date to which interval is added
0
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 500 total points
Comment Utility

If ( DateAdd("d",-6,CDate(reader("EventStartDate"))) <= StartDate And StartDate <= DateAdd("d",6,CDate(reader("EventEndDate"))) ) _

Open in new window

0
 

Author Closing Comment

by:cdemott33
Comment Utility
Thank you VERY much.  Worked perfectly!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now