How To Show Dates in 'Long Format' in ASP

Written by Nick Dunn published 8th Mar 2006 | Comment on this article

A function to display dates in their long format, including day abbreviations e.g. "th" and "nd".

How To Show Dates in Long Format in ASP

Sometimes the FormatDateTime function in VB/ASP just isn't good enough. Especially when you want a customised date format. There are numerous ways of chopping up dates, and so I've put together a simple, small function for one such format. It ensures the dates are returned with DD** - Month name - YYYY. An example being 18th February 2003. Not all date formats return the "th".

<%
Function doDate(prmDate)
Dim strDay, strDayAbbrev, strMonth, strYear

'Select the Day from the parameter
strDay = Day(prmDate)

'Set default abbreviation to th, as this is the most common
strDayAbbrev = "th"

'Change abbreviation if the value of the Day requires
If strDay = "1" Or strDay = "21" Or strDay = "31" Then strDayAbbrev = "st" End If
If strDay = "2" Or strDay = "22" Then strDayAbbrev = "nd" End If
If strDay = "3" Or strDay = "23" Then strDayAbbrev = "rd" End If

'Find the name of the month
strMonth = Monthname(Month(prmDate))

'Find the year
strYear = Year(prmDate)

'Put all of the component together
doDate = strDay & strDayAbbrev & " " & strMonth & " " & strYear

End Function
%>


To use this on an ASP page, copy and paste the above function, and where you want to display a date, use the following.
<%
Response.Write doDate(Date())
%>