function. The following code example shows various uses of the
function. The
function can format values by using both string formats and
user-defined formats.
Dim MyDateTime As Date = #1/27/2001 5:04:23 PM#
Dim MyStr As String
' The current system time is returned in the system-defined long time format.
MyStr = Format(Now(), "Long Time")
' The current system date is returned in the system-defined long date format.
MyStr = Format(Now(), "Long Date")
' The current system date is also returned in the system-defined long date
' format by using the single letter code for the format.
MyStr = Format(Now(), "D")
' The value of MyDateTime is returned in user-defined date format and in user-defined time format.
MyStr = Format(MyDateTime, "h:m:s") ' Returns "5:4:23"
MyStr = Format(MyDateTime, "hh:mm:ss tt") ' Returns "05:04:23 PM"
MyStr = Format(MyDateTime, "dddd, MMM d yyyy") ' Returns "Saturday,
' Jan 27 2001"
MyStr = Format(MyDateTime, "HH:mm:ss") ' Returns "17:04:23"
MyStr = Format(23) ' Returns "23"
' User-defined numeric formats follow.
MyStr = Format(5459.4, "##,##0.00") ' Returns "5,459.40"
MyStr = Format(334.9, "###0.00") ' Returns "334.90"
MyStr = Format(5, "0.00%") ' Returns "500.00%"