Extended ASP Date Formatting

ASP Best Practices

Format date in ASP with standard date template


Date : 2006-02-12
Unfortunetely ASP's FormatDate function leaves a lot to be desired. VBScript has enough date formatting functions but the preset formatting options are very limited.

Below you will find an example of an extended date formatting function in VBScript that should cover most of the formats that you will need.

I set my formatting strings as constants for a project so I can change them in one place if the project needs a date format change.

The key for formatting your dates is in the code as a comment because it's nice to have it with your function so you can refer to it.

Some common date formats would be:
"%dddd, %mmmm %o %yyyy" = Monday, June 5th 2006
"%mmm %dd %yyyy" = Jun 05 2006
"%mm/%dd/%yy" = 06/05/06

You get the idea, you can format the date pretty much any way you want just by passing the date and your formatting template. It's a pretty straight forward function that just brings together all of the date formating functions that do exist in VBScript in such a way that you can get the results you want with one function.

Here's the function:
Code:
<%
response.write exFormatDate(now(), "%dddd, %mmmm %dd %yyyy")

' exFormatDate function formats Date information into DateTemplate
' DateTemplate Formatting Rules
'   %m Month as a decimal no. 2
'   %mm Month as a decimal no. 02
'   %mmm Abbreviated month name Feb
'   %mmmm Full month name February
'   %d Day of the month as digits; no leading zero for single-digit days.
'   %dd Day of the month as digits; leading zero for single-digit days.
'   %ddd Day of the week as a three-letter abbreviation.
'   %dddd Day of the week as its full name.
'   %o Date of the week as Ordinal
'   %y Year as last two digits; no leading zero for years less than 10.
'   %yy Year as last two digits; leading zero for years less than 10.
'   %yyyy Year represented by four digits.
Function exFormatDate(strDate, DateTemplate)
  If not IsDate(strDate) Then
    exFormatDate = strDate
    Exit Function
  End If
  DateTemplate = replace(DateTemplate,"%mmmm",MonthName(DatePart("m",strDate),False),1,-1,0)
  DateTemplate = replace(DateTemplate,"%mmm",MonthName(DatePart("m",strDate),True),1,-1,0)
  DateTemplate = replace(DateTemplate,"%mm",right("0" & DatePart("m",strDate),2),1,-1,0)
  DateTemplate = replace(DateTemplate,"%m",DatePart("m",strDate),1,-1,0)
  DateTemplate = replace(DateTemplate,"%dddd",WeekDayName(DatePart("w",strDate,1),False),1,-1,0)
  DateTemplate = replace(DateTemplate,"%ddd",WeekDayName(DatePart("w",strDate,1),True),1,-1,0)
  DateTemplate = replace(DateTemplate,"%dd",right("0" & DatePart("d",strDate),2),1,-1,0)
  DateTemplate = replace(DateTemplate,"%d",DatePart("d",strDate),1,-1,0)
  DateTemplate = replace(DateTemplate,"%o",intToOrdinal(DatePart("d",strDate)),1,-1,0)
  DateTemplate = replace(DateTemplate,"%yyyy",DatePart("yyyy",strDate),1,-1,0)
  DateTemplate = replace(DateTemplate,"%yy",right(DatePart("yyyy",strDate),2),1,-1,0)
  DateTemplate = replace(DateTemplate,"%y",cInt(right(DatePart("yyyy",strDate),2)),1,-1,0)
  exFormatDate = DateTemplate
End Function
%>


There are still a quite a few features that could be added but this definitely gets us on our way.

You're probably looking over this code wondering where in the world the intToOrdinal function is...

intToOrdinal Function

Using ordinal date values adds a great professional touch to any site so feel free to use this date function just be sure to post back here if you find a way to improve on it.

If you pass a string that will not format as a date it will just return the string as-is. You could change that to a negative return value to be more standard but that will depend on your existing code base.

Comments :

newbie 2006-03-17 #2

Thanks,

This really makes using dates in ASP easier. Now why don't you give us exFormatTime function

  • Search For Articles