Solved

Adding and Subtracting Dates

Posted on 2009-05-13
3
246 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

831 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