DateAndTime.TimeValue(String) 方法

定義

傳回 Date 值,包含由字串表示的時間資訊,且其日期資訊設定為西元 1 年的一月 1 日。

public:
 static DateTime TimeValue(System::String ^ StringTime);
public static DateTime TimeValue (string StringTime);
static member TimeValue : string -> DateTime
Public Function TimeValue (StringTime As String) As DateTime

參數

StringTime
String

必要。 字串運算式,表示從西元 1 年的 1 月 1 日 00:00:00 到西元 9999 年 12 月 31 日 23:59:59 之間的日期/時間值。

傳回

Date 值,其中包含由字串表示的時間資訊,且其日期資訊設定為西元 1 年的 1 月 1 日。

例外狀況

StringTime 包含無效日期資訊。

範例

下列範例會 TimeValue 使用 函式將字串轉換成時間。 您也可以使用日期常值,直接將時間指派給 Date 變數。

Dim thisTime As Date
thisTime = TimeValue("4:35:17 PM")

備註

您可以使用 12 小時或 24 小時制來輸入有效時間。 例如, "2:24 P.M.""14:24" 都是有效的 StringTime 自變數。

如果自 StringTime 變數包含日期資訊, TimeValue 則不會將它包含在傳回的值中。 不過,如果 StringTime 包含無效的日期資訊,例如「1 月 32 日」 InvalidCastException ,就會發生錯誤。

適用於

另請參閱