Solved

Adding and Subtracting Dates

Posted on 2009-05-13
3
245 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
ID: 24375082
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
ID: 24375163

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
ID: 31580997
Thank you VERY much.  Worked perfectly!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now