Strings.FormatDateTime(DateTime, DateFormat) 方法

定義

傳回表示日期/時間值的字串運算式。

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

參數

Expression
DateTime

必要。 要格式化的 Date 運算式。

NamedFormat
DateFormat

選擇性。 數值,表示使用的日期/時間格式。 如果省略,則會使用 DateFormat.GeneralDate

傳回

表示日期/時間值的字串運算式。

例外狀況

NamedFormat 設定無效。

範例

這個範例示範如何使用 函 FormatDateTime 式。

' 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數據類型一律包含日期和時間資訊。 為了進行類型轉換,Visual Basic 會將 1/1/1 (1 年) 1 月 1 日視為日期的中性值,而 00:00:00 (午夜) 為時間的中性值。 如果您將 Date 值格式化為日期/時間字串, FormatDateTime 則不會在產生的字串中包含中性值。 例如,如果您將 #1/1/0001 9:30:00# 轉換成字串,結果為 “9:30:00 AM”;會隱藏日期資訊。 不過,日期資訊仍存在於原始 Date 值中,且可以使用 DatePart 之類的函式來加以復原。

注意

如果您傳遞自 Expression 變數做為 String 常值, FormatDateTime 請根據 CurrentCulture 應用程式的設定來解譯它。 不過,如果您將其傳遞為 Date 常值,請使用格式 #mm/dd/yyyy#,因為 FormatDateTime 一律會根據英文 (美國) 文化特性來解譯 Date 常值。 這是必要的,因為如果應用程式是從某個文化特性開發並使用常值進行編碼 Date ,但接著是在具有不同文化特性的平台上執行, Date 則常值可能會不正確地剖析。

NamedFormat 變數具有下列設定。

常數 描述
DateFormat.GeneralDate 顯示日期和時間。 將日期元件顯示為簡短日期。 如果有時間部分,請將它顯示為很長的時間。 如果有的話,這兩個部分都會顯示。
DateFormat.LongDate 使用計算機地區設定中指定的長日期格式顯示日期。
DateFormat.ShortDate 使用計算機地區設定中指定的簡短日期格式來顯示日期。
DateFormat.LongTime 使用計算機地區設定中指定的時間格式來顯示時間。
DateFormat.ShortTime 使用 24 小時格式顯示時間, (hh:mm) 。

適用於

另請參閱