eWebProgrammer eweb





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:
Date()

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.
DateAdd(argument,value1,value2)

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:


<HTML>
          .
          .
          .
  <%          '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
  %> 
	<!–END ASP SCRIPT ––>
  The new value is <%=dNextMonth%>.
          .
          .
</HTML>

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.
ASP Function ASP Function ASP Function ASP Function

dateadd
dateadd

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:
FormatCurrency(expression
  [,NumDigitsAfterDecimal
  [,IncludeLeadingDigit 
  [,UseParensForNegativeNumbers
  [,Groupdigits]]]])

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.

Subprocedures

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").