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 Date Formatting Function

Written by Eric Smith, Northstar Computer Systems LLC

One rule many companies are adopting prior to Y2K is that all dates show all four digits of the year, often in a MM/DD/YYYY format. The problem is that the FormatDateTime function provided in VBScript isn't overly helpful in this regard. Instead of using the built-in function, you can build your own FormatDate function, shown here:
<%
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 FormatDate(datInput, strFormat)
   Dim strResult     ' As String
   Dim intTemp       ' As Integer
   Dim strTemp       ' As String   
   strResult = strFormat
   
   strResult = Replace(strResult, "YYYY", Year(datInput), 1, -1, vbTextCompare)
   strResult = Replace(strResult, "YY", Right(Year(datInput), 2), 1, -1, vbTextCompare)

   strResult = Replace(strResult, "MM", LeftPad(Month(datInput), 2), 1, -1, vbTextCompare)
   strResult = Replace(strResult, "M", Month(datInput), 1, -1, vbTextCompare)
   
   strResult = Replace(strResult, "DD", LeftPad(Day(datInput), 2), 1, -1, vbTextCompare)
   strResult = Replace(strResult, "D", Day(datInput), 1, -1, vbTextCompare)
   
   FormatDate = strResult
End Function

%>
This will allow you to create dates that have left padded zeroes and those that don't. You can expand this function to handle month and day names; however, you'll need to be careful about evaluating the results. For instance, if you were to substitute the value 'March' for a code, the code might also substitute the month number for the first M in March.

Keywords: [ ASP Built-in Functions ]

Publication Date: 9/1/1999, Last Update: 2/13/2010