PBDR.COM

About   -   Contact   -   Purchase   -   Search   -   What's New

 
 
ASP Date/Time Formatting
Formatting dates and times in ASP can be at best basic, due to the inflexibility of the built-in FormatDateTime function which only returns short and long date and time conversions. Unlike VB's Format function, ASP does not offer an alternative where the date/time formats can be explicitly determined. Problems can arise if a European date format is required given a US date, and vice versa. These cases are common when retrieving dates from databases, usually given in a US format, where the application requirement is a European date standard. So here's a function to do just that written as an include file for ease of use. Copy to script below to a text editor and save as ConvDate.inc on your web-server.
<SCRIPT RUNAT=Server LANGUAGE="VBScript">
Function ConvDate(strDate, strFormat)

   Dim intPosItem
   Dim intHourPart
   Dim strHourPart
   Dim strMinutePart
   Dim strSecondPart
   Dim strAMPM

   If not IsDate(strDate) Then
      ConvDate = strDate
      Exit Function
   End If
	
   intPosItem = Instr(strFormat, "%m")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      DatePart("m",strDate) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%m")
   Loop

   intPosItem = Instr(strFormat, "%b")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      MonthName(DatePart("m",strDate),True) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%b")
   Loop
	
   intPosItem = Instr(strFormat, "%B")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      MonthName(DatePart("m",strDate),False) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%B")
   Loop
	
   intPosItem = Instr(strFormat, "%d")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      DatePart("d",strDate) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%d")
   Loop

   intPosItem = Instr(strFormat, "%j")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      DatePart("y",strDate) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%j")
   Loop

   intPosItem = Instr(strFormat, "%y")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      Right(DatePart("yyyy",strDate),2) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%y")
   Loop

   intPosItem = Instr(strFormat, "%Y")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      DatePart("yyyy",strDate) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%Y")
   Loop

   intPosItem = Instr(strFormat, "%w")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      DatePart("w",strDate,1) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%w")
   Loop

   intPosItem = Instr(strFormat, "%a")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      WeekDayName(DatePart("w",strDate,1),True) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%a")
   Loop

   intPosItem = Instr(strFormat, "%A")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & _
      WeekDayName(DatePart("w",strDate,1),False) & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%A")
   Loop

   intPosItem = Instr(strFormat, "%I")
   Do While intPosItem > 0
      intHourPart = DatePart("h",strDate) mod 12
      if intHourPart = 0 then intHourPart = 12
      strFormat = Left(strFormat, intPosItem-1) & _
      intHourPart & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%I")
   Loop

   intPosItem = Instr(strFormat, "%H")
   Do While intPosItem > 0
      strHourPart = DatePart("h",strDate)
      if strHourPart < 10 Then strHourPart = "0" & strHourPart
      strFormat = Left(strFormat, intPosItem-1) & _
      strHourPart & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%H")
   Loop

   intPosItem = Instr(strFormat, "%M")
   Do While intPosItem > 0
      strMinutePart = DatePart("n",strDate)
      if strMinutePart < 10 then strMinutePart = "0" & strMinutePart
      strFormat = Left(strFormat, intPosItem-1) & _
      strMinutePart & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%M")
   Loop

   intPosItem = Instr(strFormat, "%S")
   Do While intPosItem > 0
      strSecondPart = DatePart("s",strDate)
      if strSecondPart < 10 then strSecondPart = "0" & strSecondPart
      strFormat = Left(strFormat, intPosItem-1) & _
      strSecondPart & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%S")
   Loop

   intPosItem = Instr(strFormat, "%P")
   Do While intPosItem > 0
      if DatePart("h",strDate) >= 12 then
         strAMPM = "PM"
      Else
         strAMPM = "AM"
      End If
      strFormat = Left(strFormat, intPosItem-1) & strAMPM & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%P")
   Loop

   intPosItem = Instr(strFormat, "%%")
   Do While intPosItem > 0
      strFormat = Left(strFormat, intPosItem-1) & "%" & _
      Right(strFormat, Len(strFormat) - (intPosItem + 1))
      intPosItem = Instr(strFormat, "%%")
   Loop

   ConvDate = strFormat
	
End Function
</SCRIPT>

To use to include file function, add the HTML comment tag to reference the inc. file, then use the ConvDate function as defined below:

strFormattedDate = ConvDate (dtmDate, strFormat)
where dtmDate should be a Date variable
and strFormat should be a template for the output date as detailed below.

Template Description Examples:

%m Month as a decimal no. 02
%b Abbreviated month name Feb
%B Full month name February
%d Day of the month 23
%j Day of the year 54
%y Year without century 98
%Y Year with century 1998
%w Weekday as integer 5 (0 is Sunday)
%a Abbreviated day name Fri
%A Weekday Name Friday
%I Hour in 12 hour format 12
%H Hour in 24 hour format 24
%M Minute as an integer 01
%S Second as an integer 55
%P AM/PM Indicator PM
%% Actual Percent sign %%

The following example, converts a US date, dteUSDate, to a European date format, strEuroDate.

<!--#INCLUDE FILE="ConvDate.inc"-->
<%
response.write dteUSDate
strDay = ConvDate(dteUSDate, "%d")
strMonth = ConvDate(dteUSDate, "%m")
strYear = ConvDate(dteUSDate, "%Y")
strEuroDate = strDay & "/" & strMonth & "/" & strYear
response.write strEuroDate
%>
 

Top of Page

Legal Notice

Ken Howe 2011