Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cdbl date problem 1

Status
Not open for further replies.

cyberprof

Programmer
Jun 10, 2003
229
GB
I am using a form to create a date. dd/mm/yy, that date is then submitted to a new page

I am trying to convert the entered date into a number using cdbl, cdbl(datefromform), when I do this I get a type mismatch and yet cdbl(date) and cdbl(now) both work.

Is it because datefromform is a string and not a date.

How do I get round this.

Any help will be much appreciated.

Cheers J
 
Just use the Cdate function (convert date). Found on my website:


also you can use IsDate(expression) to check and make sure its a date before using cdate:
Code:
cDate(string)
isDate(string)


Something like this would do:
Code:
Dim frmDate
frmDate = Request.Form("date")


If isDate(frmDate) then
   frmDate = cDate(frmDate)
   'some other code
     Else
   'not a date do something here
End If

www.sitesd.com
ASP WEB DEVELOPMENT
 
maybe try CDbl(CDate(datefromform)) - ie, convert it into a proper date format before converting it intoa number.

Tony
________________________________________________________________________________
 
Cheers, works fine

I've been using Cdate to convert numbers to dates, I didn't know you could use it to convert strings to dates

Thanks

J
 
The problem was that while VBScript can handle single level casts internally, going from string=>date=>number was to complex a guess for it.
So treating a string as a date (without casting with cdate) will work becaue it can gues you want a date from a string that looks like a date, treating a string as a number will work if the number is numeric, etc.
Example, these are single level casts:
Code:
String=>Date: <%=Month("11/1/2004")%><br>
Date=>String: <%="blah blah blah " & Now()%><br>
String=>Number: <%="5" + 5%><br>
Number=>String: <%="5" & 5%><br>

To VBScript it is fairly obvious what we are trying to do in each of these lines and it knows what types of variables are needed for each operation. Since all variables are variants to the user, VBScript can handle simple casts to squeeze the variable into a function or operation declaration based on the requirements of that operation.

Take the first one for example. VBScript knows that the Month() function requires a single Date argument. So we pass it a variant and it attempts an internal cast on that variable to Date type. If the cast fails it gives us an error, if the cast passes then it executes the Month() method on the internally casted variable and then continues on.

The reason this only works for single level casts is that VBScript basically just checks the variables against the type of arguments it expects to see. It dos not try any complex logic to try and shoehorn the variable in by several levels of guessing at cast statements. Basically the operation looks like this:
You call a function with one variable
VBScript looks at the function definition and the type of variable expected
VBScript compares the vartype it is expecting against that that has been passed to it, and then attempts a simple cast (if necessary)
VBScript executes the method with the temporary variable it has created to hold the casted value.

Here is an example:
Code:
<%
a = "11/1/2004 5pm"
Response.Write "The length of our string is: " & len(a) & "<br>"
Response.Write "Treating our string like a date object and getting the month(): " & Month(a) & "<br>"
Response.Write "Is it still a string? The length is now: " & len(a) & "<br>"
Response.Write "The Length if it was permanently converted to date by month(): " & len(cDate("11/1/2004 5pm"))
%>

At first glance the last test (len(a)) may not seem useful since VBScript could easily convert the date to it's string representation as needed by the len() function. However if VBScript had converted the string to a date and then back again the length would also include characters for the time, as the default date=>string conversion which is why i included the final line to show that the lenth would indeed be greater had the conversion occurred.

Anyways, a bit longer of an explanation then anyone probably wanted to know, and though I have read no official documentation on it this is myunderstanding based on all of my time manipulating the language, so there may be some minor incorrect guesses as to the method of execution I listed above, but it ses to fit ith what I have observed.

-T

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top