This documentation is archived and is not being maintained.

FormatDateTime Function

Returns a string expression representing a date/time value.

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


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


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).


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


The Date data type always contains both date and time information. For purposes of type conversion, Visual Basic .NET considers 1/1/1 (January 1 of the year 1) to be a neutral value for the date, and 00:00:00 (midnight) to be a neutral value for the time. If you format a Date value as a date/time string, FormatDateTime does not include neutral values in the resulting string. For example, if you convert #1/1/0001 9:30:00# to a string, the result is "9:30:00 AM"; the date information is suppressed. However, the date information is still present in the original Date value and can be recovered with functions such as DatePart.

Note   If you pass the Expression argument as a String literal, FormatDateTime interprets it according to the CultureInfo.CurrentCulture setting of your application. However, if you pass it as a Date literal, you should always use the format #mm/dd/yyyy#, because FormatDateTime always interprets a Date literal according to the English (US) culture. This is necessary because, if an application is developed and coded using Date literals from one culture, but is then executed on a platform with a different culture, the Date literals could be parsed incorrectly.


This example demonstrates the use of the FormatDateTime function.

Dim myDate As DateTime = #3/12/1999#   ' English (US) format.
Dim myString As String
' FormatDateTime returns "Friday, March 12, 1999".
' The time information is neutral (00:00:00) and therefore suppressed.
myString = FormatDateTime(myDate, DateFormat.LongDate)


Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)

See Also

FormatCurrency Function | FormatNumber Function | FormatPercent Function | ArgumentException | DatePart Function | CultureInfo Class | CultureInfo.CurrentCulture Property