Skip navigation.
Home

Useful built-in functions in ASP

There are a lot of built-in functions in ASP. Some are for typecasting, formatting, math, date and string manipulation. Being familiar with them means saving a lot of time otherwise spent re-inventing the wheel on your own.

Typecasting in ASP

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 in a certain
way. For results, you will end up with a string. The code below will return
11/23/2003:

Response.Write FormatDatetime(date, vbshortdate)

You can also display the date like this: Sunday, November 23, 2003, using
the following code:

Response.Write FormatDatetime(date, vblongdate)

The same applies to time:

Response.Write FormatDatetime(time, vblongtime) 

Math functions

Vbscript has lots 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.

Date Functions

When working in vbscript, you will come across dates and their values.
Luckily, vbscript has a ton of built-in functions just for this.

'add a month
Response.Write DateAdd("m", 1, #12-09-1981#)
'add a year
Response.Write DateAdd("yyyy", 1, #12-09-1981#)
'add a day
Response.Write DateAdd("d", 1, #12-09-1981#)

As you can see, one month, year and day is added to the dates. Remember to
add the # character at the beginning and the end of a date. This tells vbscript
to treat the following string as a date and not as a math problem: 12/09/1981 =
12 divided by 09 divided by 1981

If you want to figure out the number of days, month or years between to
dates, you can use the DateDiff() function.

DateDiff("yyyy", #12-09-1981#, #12-09-1982#, )

The code above returns 1, since there's one year between 12-09-1981 and
12-09-1982.

If you need to obtain a value from a variable and place it into an existing
date, you can use the DateSerial() function.

Dim iday
iday = 12
Response.Write DateSerial(year(date), month(date), iday)

The following code takes the variable iday and places it into the present
month and year to create a date. As you can see from above, date returns the
present date.

To get the year of the present date, use the following code:

Response.Write year(date) 

There's also the TimeSerial() function, which is just like the
DateSerial() function, except it uses times not dates.

Now what if you want to only get a certain part of a date? You can use the
function DatePart().

DatePart("m", #12-09-1981#)

The code above will return 12 as you assumed.

If you want to get the month name instead of a number, you can call the
MonthName() function.

Response.Write monthname(12)

The code above returns December. To abbreviate this, use the argument
abbrev.

Response.Write monthname(12, true)

To get the name of the present month, do the following:

Response.Write monthname(month(date), true)

Author biography

Scott Andrew is a web programmer, and runs the site CodeHungry.