NCS Logo - Click for home page Northstar Developer Center
Platforms
All Platforms
.NET Framework (1.x - 4.x)
Active Server Pages
ASP.NET
C#
SQL Server
VB.NET
Visual Basic

Keywords
.NET Data Types
.NET E-mail
.NET Events
.NET Functions
.NET Object Programming
.NET System.Configuration
.NET System.Diagnostics
.NET System.IO
.NET System.Net
.NET System.Net.Sockets
Active Data Objects
ASP Architecture
ASP Black Belt
ASP Built-in Functions
ASP Built-in Objects
ASP Debugging
ASP Performance
ASP Security
ASP Syntax
ASP.NET Authentication
ASP.NET Controls
ASP.NET Data Access
ASP.NET Features
ASP.NET Master Pages
ASP.NET Page Events
ASP.NET Security
ASP.NET ViewState
Atom
Certifications
COM, DCOM, COM+
Data Access
E-Mail
Errors
Exporting Data
HTML Tips
IIS
Object-Oriented Programming
RSS
SQL
Uncategorized ASP Tips
VB API Programming
VB Forms
VB Syntax
XML

Book Support
Visual Basic 6 Bible
ASP Bible
ASP Weekend Crash Course
ASP.NET At Work
Creating Web Services

Building a Format Function

Written by Eric Smith, Northstar Computer Systems LLC

If you want an ASP solution for the missing Format function that you might have liked in Visual Basic, here’s a version in VBScript:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Function LeftPad
'
' This code adds leading zeroes to a number. This function is used
' by FormatDT to properly format dates and times.
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function LeftPad(intNumber, intDigits) Dim strResult Dim strTemp Dim i strTemp = CStr(intNumber) strResult = String(intDigits - Len(strTemp), "0") strResult = strResult & strTemp LeftPad = strResult End Function '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' Function FormatDT
'
' This function allows date/time values to be formatted more flexibly
' than is allowed by the built-in FormatDateTime function.
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FormatDT(datInput, strFormat) Dim strResult ' As String Dim intTemp ' As Integer Dim strTemp ' As String strResult = strFormat strResult = Replace(strResult, "#YEAR_LONG#", Year(datInput), 1, -1, vbTextCompare) strResult = Replace(strResult, "#YEAR_SHORT#", Right(Year(datInput), 2), 1, -1, vbTextCompare) strResult = Replace(strResult, "#MONTH_NAME#", MonthName(Month(datInput)), 1, -1, vbTextCompare) strResult = Replace(strResult, "#MONTH_NAME_SHORT#", Left(MonthName(Month(datInput)), 3), 1, -1, vbTextCompare) strResult = Replace(strResult, "#MONTH_2DIGIT#", LeftPad(Month(datInput), 2), 1, -1, vbTextCompare) strResult = Replace(strResult, "#MONTH_1DIGIT#", Month(datInput), 1, -1, vbTextCompare) strResult = Replace(strResult, "#DAY_2DIGIT#", LeftPad(Day(datInput), 2), 1, -1, vbTextCompare) strResult = Replace(strResult, "#DAY_1DIGIT#", Day(datInput), 1, -1, vbTextCompare) strResult = Replace(strResult, "#HOUR_2DIGIT#", LeftPad(Hour(datInput), 2), 1, -1, vbTextCompare) strResult = Replace(strResult, "#HOUR_1DIGIT#", Hour(datInput), 1, -1, vbTextCompare) strResult = Replace(strResult, "#MINUTE_2DIGIT#", LeftPad(Minute(datInput), 2), 1, -1, vbTextCompare) strResult = Replace(strResult, "#MINUTE_1DIGIT#", Minute(datInput), 1, -1, vbTextCompare) strResult = Replace(strResult, "#SECOND_2DIGIT#", LeftPad(Second(datInput), 2), 1, -1, vbTextCompare) strResult = Replace(strResult, "#SECOND_1DIGIT#", Second(datInput), 1, -1, vbTextCompare) FormatDT = strResult End Function
You can additional formatting options as long as the name is unique. The LeftPad function allows you to create times like 05:04:02 using the _2DIGIT tags.

Keywords: [ ASP Black Belt | ASP Built-in Functions ]

Publication Date: 2/1/2000, Last Update: 3/3/2010