0

In classic ASP, I am unable to use DateAdd() with variables. I see no reason why this should work.

strTargetDate=DateAdd("d",visitDate,followDate)

visitDate is an incremental value -- 30, 60, 90, 180 etc. followDate is an actual date. However, I receive a type mismatch error using this code. Shouldn't this work??

4
  • @KenWhite VBScript is weakly typed (types are determined by assigned values) and does not obey type prefixes. Except for fixed arrays assignment never cause type errors. Commented Oct 23, 2013 at 14:53
  • When you say "unable", do you mean you are getting an error or an unexpected result? Edit your question to include the relevant information for either/both options. Commented Oct 23, 2013 at 14:54
  • @Ekkehard.Horner: Who said anything about 'type prefixes'? Variable names are a convenience to the programmer; I'm not aware of any language that actually uses type prefixes ("Hungarian notation") to determine variable types. Commented Oct 23, 2013 at 15:23
  • AtEveryBodyExceptKenWhite - don't try to make sense of KenWhite's and my comments (or my reference to his speculations in my answer), because he choose to delete his contributions I was refering to. Commented Oct 23, 2013 at 15:38

1 Answer 1

2

Either visitDate isn't/can't be converted to a number, or followDate isn't/can't be converted to a date. So check the TypeName() of your input and pay attention to date formats.

Partly to show facts against @Ken's speculations:

>> s = "string"
>> WScript.Echo 0, s, TypeName(s)
>> s = DateAdd("d", 1, Now)
>> WScript.Echo 1, s, TypeName(s)
>> s = DateAdd("d", "1", CStr(now))
>> WScript.Echo 2, s, TypeName(s)
>> s = DateAdd("d", 1, "20/10/2013")
>>
0 string String
1 24.10.2013 17:05:54 Date
2 24.10.2013 17:05:54 Date
>> s = DateAdd("d", 1, "32.13.1")
>>
Error Number:       13
Error Description:  Type mismatch

Update wrt comment:

As computations involving Null should propagate Null, this

>> WScript.Echo TypeName(DateAdd("d", 1, Null))
>>
Null
>>

is no surprise. While you should handle Nulls for your DateAdd() in a way appropriate to your application, they are not the cause for the type mismatch.

Empty strings (""), however, could well be the culprits:

>> WScript.Echo TypeName(DateAdd("d", 1, ""))
>>
Error Number:       13
Error Description:  Type mismatch
>>
Sign up to request clarification or add additional context in comments.

6 Comments

Im receiving a type mismatch on followDate. Would null DB entries in the table cause that?
@SteveWeaver Please follow the advice you were given: check the TypeName of your two input values.
Both return as a string type. I just checked both. Will DateAdd not accept two string inputs? Also both variables return values. followDate returns the number 60, visitDate is returning a valid date.
@SteveWeaver Then your arguments are in the wrong order. Since you're calling DateAdd("d", visitDate, followDate), visitDate should have the value 60, and followDate should have the date string.
Man I really hope thats not what I did. Ill check again as soon as I get to work in the morning and let you know.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.