Share via


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

See Also

Working with Dates and Times | The Date Data Type | Getting the Current Date and Time | Date Delimiters | Assembling a Date | Getting Part of a Date | Adding and Subtracting Dates | Calculating Elapsed Time