Format Date Time(DateTime, DateFormat) Method
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a string expression representing a date/time value.
public static string FormatDateTime (DateTime Expression, Microsoft.VisualBasic.DateFormat NamedFormat = Microsoft.VisualBasic.DateFormat.GeneralDate);
static member FormatDateTime : DateTime * Microsoft.VisualBasic.DateFormat -> string
Public Function FormatDateTime (Expression As DateTime, Optional NamedFormat As DateFormat = Microsoft.VisualBasic.DateFormat.GeneralDate) As String
Date expression to be formatted.
Optional. Numeric value that indicates the date/time format used. If omitted,
DateFormat.GeneralDate is used.
A string expression representing a date/time value.
NamedFormat setting is not valid.
This example demonstrates the use of the
' English (US) format. Dim testDate As DateTime = #3/12/1999# ' FormatDateTime returns "Friday, March 12, 1999". ' The time information is neutral (00:00:00) and therefore suppressed. Dim testString As String = FormatDateTime(testDate, DateFormat.LongDate)
Date data type always contains both date and time information. For purposes of type conversion, Visual Basic 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
If you pass the
Expression argument as a
FormatDateTime interprets it according to the CurrentCulture setting of your application. However, if you pass it as a
Date literal, 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.
NamedFormat argument has the following settings.
||Display a date and/or time. Display a date part as a short date. If there is a time part, display it as a long time. If present, both parts display.|
||Display a date using the long date format specified in your computer's regional settings.|
||Display a date using the short date format specified in your computer's regional settings.|
||Display a time using the time format specified in your computer's regional settings.|
||Display a time using the 24-hour format (hh:mm).|
- Format(Object, String)
- FormatCurrency(Object, Int32, TriState, TriState, TriState)
- FormatNumber(Object, Int32, TriState, TriState, TriState)
- FormatPercent(Object, Int32, TriState, TriState, TriState)
- DatePart(DateInterval, DateTime, FirstDayOfWeek, FirstWeekOfYear)
- String Manipulation Summary