Microsoft Office 2000/Visual Basic Programmer's Guide   

Formatting a Date

You can use predefined formats to format a date by calling the FormatDateTime function, or you can create a custom format for a date by using the Format function.

The following procedure formats a date by using both built-in and custom formats:

Sub DateFormats(Optional dteDate As Date)
   ' This procedure formats a date using both built-in
   ' and custom formats.
   
   ' If dteDate argument has not been passed, then
   ' dteDate is initialized to 0 (or December 30, 1899,
   ' the date equivalent of 0).
   If CLng(dteDate) = 0 Then
      ' Use today's date.
      dteDate = Now
   End If

   ' Print date in built-in and custom formats.
   Debug.Print FormatDateTime(dteDate, vbGeneralDate)
   Debug.Print FormatDateTime(dteDate, vbLongDate)
   Debug.Print FormatDateTime(dteDate, vbShortDate)
   Debug.Print FormatDateTime(dteDate, vbLongTime)
   Debug.Print FormatDateTime(dteDate, vbShortTime)
   Debug.Print Format$(dteDate, "ddd, mmm d, yyyy")
   Debug.Print Format$(dteDate, "mmm d, H:MM am/pm")
End Sub

The DateFormats procedure is available in the modDateTime module in VBA.mdb in the ODETools\V9\Samples\OPG\Samples\CH07 subfolder on the Office 2000 Developer CD-ROM.