Type Checking
These functions allow you to figure out data types.
TypeName()
Returns the data type rather than the code.
IsNumeric()
Returns a boolean value of true if the data type is that of a number and false if otherwise.
IsArray()
Returns a boolean value of true if the data type is that of an array and false if otherwise.
IsDate()
Returns a boolean value of true if the data type is that of a date and false if otherwise.
IsEmpty()
Returns a boolean value of true if the data type is a empty value and false if otherwise.
IsNull()
Returns a boolean value of true if the data type contains no valid data and false if otherwise.
IsObject()
Returns a boolean value of true if the data type is that of an object and false if otherwise.
Typecasting
What's typecasting? Typecasting converts between data types. For instance, if you have data you obtained from a form, it's considered text. However, with typecasting, you can convert that variable into a date value.
Dim idate
'Get the DOB from the form
idate = Request.Form("DOB")
'Convert it to a date
idate = cdate(idate)
With the CBool() function, you can get a boolean value. If the number is 0, you get a boolean value of false, otherwise you get a boolean value of true. The following code will return true.
Dim inumber
inumber = 12
If CBool(inumber) = True Then
Response.Write "True"
Else
Response.Write "False"
End If
Formatting Functions
Formatting functions are useful when you need to display data the way you want it. For results, you will end up with a string. The code below will return 4/6/2004
Response.Write FormatDatetime(date, vbshortdate)
You can also display the date like this: Tuesday, April 06, 2004, using the following code:
Response.Write FormatDatetime(date, vblongdate)
The same applies to time:
Response.Write FormatDatetime(time, vblongtime)
Math functions
Vbscript has lot's of math functions, however, you won't use them very often, so I will only list a couple.
Rnd(), when used with randomize, will generate an random number (random enough) less the one and greater than or equal to zero. Try out the following code:
randomize
Response.Write rnd
What does randomize do? Randomize uses the system timer to start the random number generator.
Another vbscript math function you might run into is the Round() function. And as you would guess, it rounds numbers.
Round(9.99)
The code above returns 10.