Click here to Skip to main content
13,352,770 members (40,959 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 5 May 2008

ASP FormatDate

, 13 Feb 2009
Rate this:
Please Sign up or sign in to vote.
Date Format function for VBScript / Classic ASP


Visual Basic's Format function for formatting dates is missing from VBScript.

Without Format, it can be difficult to yield custom formatted dates.

Using the FormatDate function described below, your worries are over!

Using the Code

First find the format string's length.

Next, loop through every character in the format string.

Begin building a format block.

Handle the format block when the character changes (meaning either a new format block is starting, or we need to persist a separator character).

The various supported format blocks are translated through the SELECT CASE statement and the output string (formatted date) is appended. The CASE ELSE statement at the end will append separator characters or anything not understood as a format block string.

Keep looping until we are one past the end, and the final block or character will be handled.

Function FormatDate(strDate, strDateFmt)
  dim strRet
  dim i  
  dim formatBlock
  dim formatLength
  dim charLast
  dim charCur
  formatLength = len(strDateFmt)
  for i = 1 to formatLength + 1
    ' grab the current character
    charCur = mid(strDateFmt, i, 1)
    if charCur = charLast then
        ' The block is not finished. 
        ' Continue growing the block and iterate to the next character.
        formatBlock = formatBlock & charCur
        ' we have a change and need to handle the previous block
        select case formatBlock
        case "mmmm"
            strRet = strRet & MonthName(DatePart("m",strDate),False)
        case "mmm"
            strRet = strRet & MonthName(DatePart("m",strDate),True)
        case "mm"
            strRet = strRet & right("0" & DatePart("m",strDate),2)
        case "m"
            strRet = strRet & DatePart("m",strDate)
        case "dddd"
            strRet = strRet & WeekDayName(DatePart("w",strDate,1),False)
        case "ddd"
            strRet = strRet & WeekDayName(DatePart("w",strDate,1),True)
        case "dd"
            strRet = strRet & right("0" & DatePart("d",strDate),2)
        case "d"
            strRet = strRet & DatePart("d",strDate)
        case "o"
            strRet = strRet & intToOrdinal(DatePart("d",strDate))
        case "yyyy"
            strRet = strRet & DatePart("yyyy",strDate)
        case "yy"
            strRet = strRet & right(DatePart("yyyy",strDate),2)
        case "y"
            strRet = strRet & cInt(right(DatePart("yyyy",strDate),2))
        case else
            strRet = strRet & formatBlock
        end select
        ' Block handled.  
        ' Now reset the block and continue iterating to the next character.
        formatBlock = charCur
    end if
    charLast = charCur
  next 'i
  FormatDate = strRet
End Function


  • 5/5/2008 v1.0
  • 2/13/2009 v2.0


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) AccessVia, Inc.
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGood Work Pin
Dhillon19-Feb-09 11:19
memberDhillon19-Feb-09 11:19 
GeneralGreater example Pin
Dieter_Van_Vaalwater13-Feb-09 10:52
memberDieter_Van_Vaalwater13-Feb-09 10:52 
GeneralCool! Pin
Member 464791429-Jun-08 16:17
memberMember 464791429-Jun-08 16:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 13 Feb 2009
Article Copyright 2008 by amattice
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid