Visual Basic Language Reference  

FormatDateTime Function

Returns an expression formatted as a date or time.

Function FormatDateTime(
   ByVal Expression As DateTime,
   Optional ByVal NamedFormat As DateFormat = DateFormat.GeneralDate
) As String

Parameters

Expression
Required. Date expression to be formatted.
NamedFormat
Optional. Numeric value that indicates the date or time format used. If omitted, GeneralDate is used.

Settings

The NamedFormat argument has the following settings:

Constant Description
DateFormat.GeneralDate Display a date and/or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed.
DateFormat.LongDate Display a date using the long date format specified in your computer's regional settings.
DateFormat.ShortDate Display a date using the short date format specified in your computer's regional settings.
DateFormat.LongTime Display a time using the time format specified in your computer's regional settings.
DateFormat.ShortTime Display a time using the 24-hour format (hh:mm).

Exceptions/Errors

Exception type Error number Condition
ArgumentException 5 NamedFormat setting is invalid.

Example

This example demonstrates the use of the FormatDateTime function.

Dim myDate As DateTime = #2/14/89#
Dim myString As String
' Returns "Tuesday, February 14, 1989".
myString = FormatDateTime(myDate, DateFormat.LongDate)

See Also

FormatCurrency Function | FormatNumber Function | FormatPercent Function | ArgumentException