ASP   «Prev  Next»

Lesson 7ASP built-in Functions
ObjectiveUse built-in and user-defined functions for data manipulation.

ASP built-in Functions

You will often need to manipulate the values of variables.
VBScript provides a number of functions for this purpose, including a Replace function that replaces every occurrence of a substring[1] (word or phrase) in another string with a replacement string.
The Trim functions remove leading spaces (LTrim), trailing spaces (RTrim) or both (Trim) from a string.
Be aware that different functions work with different types of data.

Date and Time Functions

Two of the most useful Date and Time functions are:

The Date function returns the current date. The date returned is the date on the server. This value can be stored to a variable and manipulated.


The DateAdd function adds dates together. It can also be used to increment or decrement a date by an amount in any time or date unit.
Here is the DateAdd function used to add 30 days to the current date:

  <%          'BEGIN ASP SCRIPT
  Dim dCurrDate, dNextMonth          
  'Initialize a couple of date variables
  dCurrDate = Date() 
  'dCurrDate is now equal to today's date
  dNextMonth = DateAdd("d", 30, dCurrDate) 
  'Add 30 days to dCurrDate
  The new value is <%=dNextMonth%>.

This MouseOver will give you a line-by-line explanation of how we are using the DateAdd function and what values we need to set in its parameters to add 30 days to the current date.

Elements of the ASP script
  1. Create and initialize the two variables, dCurrDate and dNextMonth, that we need to perform the calculation.
  2. Use the Date() function for current date to set the value of variable dCurrDate to today's date.
  3. Use the DateAdd function with its parameter set to days, +30 and today's date to count 30 days past today and place the result in our other variable, dNextMonth. Note that the result date may or may not be in the same month as today's date.
  4. Display the new date, 30 days in the future to the user

Date Add Function Example

Numeric functions

Several functions that format a number are available to you. FormatCurrency returns the number with a leading currency symbol. FormatPercent returns the number with a following % symbol and multiplies the value by 100 to create a percentage. FormatNumber returns the number itself with no leading or following characters. The function to format a number as currency has this syntax:


The other numeric functions follow an identical syntax.
You can format the same numeric value in different ways and define some additional information on the functions' parameters.
The ASP statements and functions in these lessons are just a part of what is available for your Web applications' use through VBScript.


User-defined subprocedures and functions let you reuse code you have already written.
With these "mini-programs", you can create generic code that can be easily adapted from use in one Web site to another.

[1]Substring: A portion of a character string (e.g., "shirt" is a substring of "t-shirt").