Strings.FormatDateTime(DateTime, DateFormat) 方法


返回一个表示日期/时间值的字符串表达式。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



必需。Required. 要格式化的 Date 表达式。Date expression to be formatted.


可选。Optional. 指示所使用的日期/时间格式的数值。Numeric value that indicates the date/time format used. 如果省略,则使用 DateFormat.GeneralDateIf omitted, DateFormat.GeneralDate is used.


一个表示日期/时间值的字符串表达式。A string expression representing a date/time value.


NamedFormat 设置无效。NamedFormat setting is not valid.


此示例演示如何使用 FormatDateTime 函数。This example demonstrates the use of the FormatDateTime function.

' 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 数据类型始终包含日期和时间信息。The Date data type always contains both date and time information. 出于类型转换的目的,Visual Basic 将1/1/1 (第1年1月1日)视为日期的非特定值,00:00:00 (午夜)为时间的非特定值。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. 如果将 Date 值设置为日期/时间字符串格式,则 FormatDateTime 不会在生成的字符串中包含非特定值。If you format a Date value as a date/time string, FormatDateTime does not include neutral values in the resulting string. 例如,如果将 #1/1/0001 9:30: 00 # 转换为字符串,则结果为 "9:30:00 AM";禁止显示日期信息。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. 但是,日期信息仍以原始 Date 值形式存在,可以通过 DatePart等功能进行恢复。However, the date information is still present in the original Date value and can be recovered with functions such as DatePart.


如果将 Expression 参数作为 String 文本传递,FormatDateTime 会根据应用程序的 CurrentCulture 设置对其进行解释。If you pass the Expression argument as a String literal, FormatDateTime interprets it according to the CurrentCulture setting of your application. 但是,如果将它作为 Date 文本传递,请使用格式 #mm/dd/yyyy #,因为 FormatDateTime 始终根据英语(美国)区域性解释 Date 文本。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. 这是必需的,因为如果应用程序是使用 Date 某个区域性的文本进行开发和编码的,但随后在具有不同区域性的平台上执行,则 Date 文本可能会错误地进行分析。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 参数具有以下设置。The NamedFormat argument has the following settings.

返回的常量Constant 描述Description
DateFormat.GeneralDate 显示日期和/或时间。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.
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 使用24小时格式显示时间(hh: mm)。Display a time using the 24-hour format (hh:mm).