TexasWebDevelopers.com Home Page

Formatting ASP

The Format function formats a number, date or time based on the user entered
format type. There are two required statements: expression and format.

Expression represents the number, date or time you are trying to format.

The format argument tells the system how to display the input expression.
Acceptable values for the format argument are: Short Time, Long Time, Short
Date, Long Date, General Date, General Number, Currency, Fixed, Standard,
Percent, Yes/No, True/False, or On/Off.

syntax:
string = Format(expression, format)

Example usage:
<%
' declare and set variables
dim num, datetime
num = 1741534.542058484
datetime = Now()
%>
Short Time:
<% = Format( datetime, "Short Time" ) %><BR>
Long Time:
<% = Format( datetime, "Long Time" ) %><BR>
Short Date:
<% = Format( datetime, "Short Date" ) %><BR>
Long Date:
<% = Format( datetime, "Long Date" ) %><BR>
General Date:
<% = Format( datetime, "General Date" ) %><BR>
General Number:
<% = Format( num, "General Number" ) %><BR>
Currency:
<% = Format( num, "Currency" ) %><BR>
Fixed:
<% = Format( num, "Fixed" ) %><BR>
Standard:
<% = Format( num, "Standard" ) %><BR>
Percent:
<% = Format( num, "Percent" ) %><BR>
Yes/No:
<% = Format( num, "Yes/No" ) %><BR>
True/False:
<% = Format( num, "True/False" ) %><BR>
On/Off:
<% = Format( num, "On/Off" ) %><BR>

Actual results:

Short Time: 22:19
Long Time: 10:19:52 PM
Short Date: 5/16/2008
Long Date: Friday, May 16, 2008
General Date: 5/16/2008 10:19:52 PM
General Number: 1741534.54205848
Currency: $1,741,534.54
Fixed: 1741534.54
Standard: 1,741,534.54
Percent: 174,153,454.21%
Yes/No: Yes
True/False: True
On/Off: On
Source code:
<%
Private Function Format(byVal expression, byVal strFormat)
On Error Resume Next
Select Case lcase( strFormat )
Case "general date"
Format = FormatDateTime(expression, 0)
Case "long date"
Format = FormatDateTime(expression, 1)
Case "short date"
Format = FormatDateTime(expression, 2)
Case "long time"
Format = FormatDateTime(expression, 3)
Case "short time"
Format = FormatDateTime(expression, 4)
Case "general number"
Format = Replace( expression, ",", "" )
Case "currency"
Format = FormatCurrency(expression, 2)
Case "fixed"
Format = Replace( FormatNumber(expression, _
2, -1), ",", "" )
Case "standard"
Format = FormatNumber(expression, 2, -1)
Case "percent"
Format = FormatPercent(expression, 2)
Case "yes/no"
expression = cLng(expression)
If expression = 0 then
Format = "No"
else
Format = "Yes"
end if
Case "true/false"
expression = cLng(expression)
If expression = 0 then
Format = "False"
else
Format = "True"
end if
Case "on/off"
expression = cLng(expression)
If expression = 0 then
Format = "Off"
else
Format = "On"
end if
Case Else
Format = expression
End Select
On Error GoTo 0
End Function
%>