This article originally appeared on 4GuysFromRolla.com at www.4guysfromrolla.com/webtech/051601-1.shtml.
When creating ASP pages you can choose to use a number of scripting languages, such as VBScript, JScript, PerlScript, and even Python. While this may seem all fine and good, realize that VBScript, the most popular server-side scripting languages, is a watered-down version of VisualBasic. Think of VBScript as VB-Lite - certain things are missing.
VB’s Format() function is such a missing feature from VBScript. Yeah, VBScript contains a number of formatting functions - FormatDateTime(), FormatNumber(), FormatCurrency(), etc. - but these format functions pale in comparison to the more generic Format() function. These VBScript formatting functions offer just a select few formatting “templates” and are also reliant on the locale settings of the Web server. The VB Format() function, however, is much more generic, allowing for a much more custom format.
The Format() function takes two parameters and returns a string. The first parameter is the string to format, while the second parameter is the format pattern string. (The returned string is the first parameter that is formatted according to the format pattern.)
The FormatPattern string can contain a wide-array of predefined format patterns. You can see a long list of these patterns here. Below you will see some common format string examples and their associated outputs. You can also view the live demo and try out your own formatting patterns and inputs and view the formatted output!
Due to the wide array of potential format patterns, the Format() function can be used to format a number of string types. Often, the Format() function is used for formatting dates. So, if today is May 1st, 2001:
The Format() function will also format any number:
Check out these links for more about the Format() function and formatting patterns:
- List of formatting options
- More thorough list of formatting pattern options
- The Format() Function Technical Docs
Let’s take this last Format() function example, formatting a string of numbers into a telephone number. Since the Format() function is a VB function (and not a VBScript function), you can’t use the Format() function in an ASP page. So, if you wanted the same output results for the telephone number formatting using VBScript you would have to do:
Not very pretty, but it works. Recently I found myself creating a week-view calendar for viewing scheduled DIRECTV installations for < shameless plug >InfoDish.com< /shameless plug >. Since there were a number of areas on the Web site that I needed to have very custom control over the date formatting, I decided that I wasn’t interested in using VBScript’s FormatDateTime() function. I considered using a custom date formatting script, such as the one presented in the 4Guys article A Customizable Date Formatting Routine, but decided that it would be nice to be able to also have more control over other formatting facets (such as complete control over formatting numbers). What I really wanted was to be able to easily use VB’s Format() function via an ASP page…
Then a light bulb went off. Since ASP supports the use of ActiveX components (COM components) I could simply create an ActiveX component for any function available in Visual Basic! (If you want a good article on creating an ActiveX DLLs in Visual Basic for use on an ASP web site, read Writing a COM Object with VisualBasic 6.0.)
In simple terms, open up Visual Basic, click New, and select ‘ActiveX DLL’. Click Project -> Add New Class Module. Then add this code to that module.
Public Function FormatString(ByVal Str As String, ByVal Formatting as String) As String FormatString = Format(Str, Formatting) End Function Compile the project, write it to a DLL, save it to the server, and register it. (You can download the DLL file and complete VisualBasic Project files at the end of this article.) The code to use the VB Format() function via an ASP page is now only three lines long:
Now, you are free to use VB’s powerful Format() function using VBScript! Be sure to view the live demo! I hope this helps you in your projects.
God bless and Happy Programming,