TimeSpan.TryParse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將指定時間區間的字串表示轉換為其 TimeSpan 等價值,並回傳一個表示轉換是否成功的值。
多載
| 名稱 | Description |
|---|---|
| TryParse(ReadOnlySpan<Char>, TimeSpan) |
將時間區間的跨度表示轉換為等 TimeSpan 價值,並回傳一個表示轉換是否成功的值。 |
| TryParse(String, TimeSpan) |
將時間區間的字串表示轉換為其 TimeSpan 對應值,並回傳一個表示轉換是否成功的值。 |
| TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan) |
利用指定的文化特定格式資訊,將時間區間的跨度表示轉換為其 TimeSpan 等效值,並回傳一個表示轉換是否成功的值。 |
| TryParse(String, IFormatProvider, TimeSpan) |
利用指定的文化特定格式資訊,將時間區間的字串表示轉換為 TimeSpan 等價值,並回傳一個表示轉換是否成功的值。 |
TryParse(ReadOnlySpan<Char>, TimeSpan)
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
將時間區間的跨度表示轉換為等 TimeSpan 價值,並回傳一個表示轉換是否成功的值。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParse(ReadOnlySpan<char> s, out TimeSpan result);
static member TryParse : ReadOnlySpan<char> * TimeSpan -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As TimeSpan) As Boolean
參數
- s
- ReadOnlySpan<Char>
一個包含代表要轉換時間區間字元的跨度。
傳回
true若s成功轉換;否則,。 false 若false參數為snull或,格式無效,代表時間區間小於 Empty 或大於 TimeSpan.MaxValue,或至少有一個天、小時、分鐘或秒的成分超出有效範圍,則此操作會回傳。
適用於
TryParse(String, TimeSpan)
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
將時間區間的字串表示轉換為其 TimeSpan 對應值,並回傳一個表示轉換是否成功的值。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParse(string s, out TimeSpan result);
public static bool TryParse(string? s, out TimeSpan result);
static member TryParse : string * TimeSpan -> bool
Public Shared Function TryParse (s As String, ByRef result As TimeSpan) As Boolean
參數
- s
- String
字串,指定要轉換的時間間隔。
傳回
true若s成功轉換;否則,。 false 若false參數為snull或,格式無效,代表時間區間小於 Empty 或大於 TimeSpan.MaxValue,或至少有一個天、小時、分鐘或秒的成分超出有效範圍,則此操作會回傳。
範例
以下範例使用該TryParse方法從有效TimeSpan字串建立TimeSpan物件,並表示解析操作失敗,因為時間跨度字串無效。
using System;
public class TryParse
{
private static void ParseTimeSpan(string intervalStr)
{
// Write the first part of the output line.
Console.Write( "{0,20} ", intervalStr );
// Parse the parameter, and then convert it back to a string.
TimeSpan intervalVal;
if (TimeSpan.TryParse(intervalStr, out intervalVal))
{
string intervalToStr = intervalVal.ToString();
// Pad the end of the TimeSpan string with spaces if it
// does not contain milliseconds.
int pIndex = intervalToStr.IndexOf(':');
pIndex = intervalToStr.IndexOf('.', pIndex);
if (pIndex < 0)
intervalToStr += " ";
Console.WriteLine("{0,21}", intervalToStr);
// Handle failure of TryParse method.
}
else
{
Console.WriteLine("Parse operation failed.");
}
}
public static void Main()
{
Console.WriteLine( "{0,20} {1,21}",
"String to Parse", "TimeSpan" );
Console.WriteLine( "{0,20} {1,21}",
"---------------", "---------------------" );
ParseTimeSpan("0");
ParseTimeSpan("14");
ParseTimeSpan("1:2:3");
ParseTimeSpan("0:0:0.250");
ParseTimeSpan("10.20:30:40.50");
ParseTimeSpan("99.23:59:59.9999999");
ParseTimeSpan("0023:0059:0059.0099");
ParseTimeSpan("23:0:0");
ParseTimeSpan("24:0:0");
ParseTimeSpan("0:59:0");
ParseTimeSpan("0:60:0");
ParseTimeSpan("0:0:59");
ParseTimeSpan("0:0:60");
ParseTimeSpan("10:");
ParseTimeSpan("10:0");
ParseTimeSpan(":10");
ParseTimeSpan("0:10");
ParseTimeSpan("10:20:");
ParseTimeSpan("10:20:0");
ParseTimeSpan(".123");
ParseTimeSpan("0.12:00");
ParseTimeSpan("10.");
ParseTimeSpan("10.12");
ParseTimeSpan("10.12:00");
}
}
// String to Parse TimeSpan
// --------------- ---------------------
// 0 00:00:00
// 14 14.00:00:00
// 1:2:3 01:02:03
// 0:0:0.250 00:00:00.2500000
// 10.20:30:40.50 10.20:30:40.5000000
// 99.23:59:59.9999999 99.23:59:59.9999999
// 0023:0059:0059.0099 23:59:59.0099000
// 23:0:0 23:00:00
// 24:0:0 Parse operation failed.
// 0:59:0 00:59:00
// 0:60:0 Parse operation failed.
// 0:0:59 00:00:59
// 0:0:60 Parse operation failed.
// 10: Parse operation failed.
// 10:0 10:00:00
// :10 Parse operation failed.
// 0:10 00:10:00
// 10:20: Parse operation failed.
// 10:20:0 10:20:00
// .123 Parse operation failed.
// 0.12:00 12:00:00
// 10. Parse operation failed.
// 10.12 Parse operation failed.
// 10.12:00 10.12:00:00
open System
let parseTimeSpan intervalStr =
// Write the first part of the output line.
printf $"%20s{intervalStr} "
// Parse the parameter, and then convert it back to a string.
match TimeSpan.TryParse intervalStr with
| true, intervalVal ->
let intervalToStr = string intervalVal
// Pad the end of the TimeSpan string with spaces if it
// does not contain milliseconds.
let pIndex = intervalToStr.IndexOf ':'
let pIndex = intervalToStr.IndexOf('.', pIndex)
let intervalToStr =
if pIndex < 0 then
intervalToStr + " "
else intervalStr
printfn $"{intervalToStr,21}"
// Handle failure of TryParse method.
| _ ->
printfn "Parse operation failed."
printfn "%20s %21s" "String to Parse" "TimeSpan"
printfn "%20s %21s" "---------------" "---------------------"
parseTimeSpan "0"
parseTimeSpan "14"
parseTimeSpan "1:2:3"
parseTimeSpan "0:0:0.250"
parseTimeSpan "10.20:30:40.50"
parseTimeSpan "99.23:59:59.9999999"
parseTimeSpan "0023:0059:0059.0099"
parseTimeSpan "23:0:0"
parseTimeSpan "24:0:0"
parseTimeSpan "0:59:0"
parseTimeSpan "0:60:0"
parseTimeSpan "0:0:59"
parseTimeSpan "0:0:60"
parseTimeSpan "10:"
parseTimeSpan "10:0"
parseTimeSpan ":10"
parseTimeSpan "0:10"
parseTimeSpan "10:20:"
parseTimeSpan "10:20:0"
parseTimeSpan ".123"
parseTimeSpan "0.12:00"
parseTimeSpan "10."
parseTimeSpan "10.12"
parseTimeSpan "10.12:00"
// String to Parse TimeSpan
// --------------- ---------------------
// 0 00:00:00
// 14 14.00:00:00
// 1:2:3 01:02:03
// 0:0:0.250 00:00:00.2500000
// 10.20:30:40.50 10.20:30:40.5000000
// 99.23:59:59.9999999 99.23:59:59.9999999
// 0023:0059:0059.0099 23:59:59.0099000
// 23:0:0 23:00:00
// 24:0:0 Parse operation failed.
// 0:59:0 00:59:00
// 0:60:0 Parse operation failed.
// 0:0:59 00:00:59
// 0:0:60 Parse operation failed.
// 10: Parse operation failed.
// 10:0 10:00:00
// :10 Parse operation failed.
// 0:10 00:10:00
// 10:20: Parse operation failed.
// 10:20:0 10:20:00
// .123 Parse operation failed.
// 0.12:00 12:00:00
// 10. Parse operation failed.
// 10.12 Parse operation failed.
// 10.12:00 10.12:00:00
Module TryParse
Sub ParseTimeSpan(intervalStr As String)
' Write the first part of the output line.
Console.Write("{0,20} ", intervalStr)
' Parse the parameter, and then convert it back to a string.
Dim intervalVal As TimeSpan
If TimeSpan.TryParse(intervalStr, intervalVal) Then
Dim intervalToStr As String = intervalVal.ToString()
' Pad the end of the TimeSpan string with spaces if it
' does not contain milliseconds.
Dim pIndex As Integer = intervalToStr.IndexOf(":"c)
pIndex = intervalToStr.IndexOf("."c, pIndex)
If pIndex < 0 Then intervalToStr &= " "
Console.WriteLine("{0,21}", intervalToStr)
' Handle failure of TryParse method.
Else
Console.WriteLine("Parse operation failed.")
End If
End Sub
Public Sub Run()
Console.WriteLine("{0,20} {1,21}",
"String to Parse", "TimeSpan")
Console.WriteLine("{0,20} {1,21}",
"---------------", "---------------------")
ParseTimeSpan("0")
ParseTimeSpan("14")
ParseTimeSpan("1:2:3")
ParseTimeSpan("0:0:0.250")
ParseTimeSpan("10.20:30:40.50")
ParseTimeSpan("99.23:59:59.9999999")
ParseTimeSpan("0023:0059:0059.0099")
ParseTimeSpan("23:0:0")
ParseTimeSpan("24:0:0")
ParseTimeSpan("0:59:0")
ParseTimeSpan("0:60:0")
ParseTimeSpan("0:0:59")
ParseTimeSpan("0:0:60")
ParseTimeSpan("10:")
ParseTimeSpan("10:0")
ParseTimeSpan(":10")
ParseTimeSpan("0:10")
ParseTimeSpan("10:20:")
ParseTimeSpan("10:20:0")
ParseTimeSpan(".123")
ParseTimeSpan("0.12:00")
ParseTimeSpan("10.")
ParseTimeSpan("10.12")
ParseTimeSpan("10.12:00")
End Sub
End Module
' This example generates the following output:
' String to Parse TimeSpan
' --------------- ---------------------
' 0 00:00:00
' 14 14.00:00:00
' 1:2:3 01:02:03
' 0:0:0.250 00:00:00.2500000
' 10.20:30:40.50 10.20:30:40.5000000
' 99.23:59:59.9999999 99.23:59:59.9999999
' 0023:0059:0059.0099 23:59:59.0099000
' 23:0:0 23:00:00
' 24:0:0 Parse operation failed.
' 0:59:0 00:59:00
' 0:60:0 Parse operation failed.
' 0:0:59 00:00:59
' 0:0:60 Parse operation failed.
' 10: Parse operation failed.
' 10:0 10:00:00
' :10 Parse operation failed.
' 0:10 00:10:00
' 10:20: Parse operation failed.
' 10:20:0 10:20:00
' .123 Parse operation failed.
' 0.12:00 12:00:00
' 10. Parse operation failed.
' 10.12 Parse operation failed.
' 10.12:00 10.12:00:00
備註
TryParse(System.String,System.TimeSpan@) 方法
TimeSpan.TryParse(String, TimeSpan) 方法就像 TimeSpan.Parse(String) 方法,不同之處在於,如果轉換失敗,則不會擲回例外狀況。
s 參數包含的時間間隔格式規範:
[ws][-]{ d | d.hh:mm[:ss[.ff]] | hh:mm[:ss[.ff]] }[ws]
方括弧 ([ 和 ]) 中的元素是選擇性的。 必須從大括號({ 與 })括住並以垂直線(|)分隔的選項清單中選取一個。 下表說明每個元素。
| 元素 | Description |
|---|---|
| ws | 選擇性的空格符。 |
| - | 選擇性減號,表示負數 TimeSpan。 |
| d | 天,範圍從 0 到 10675199。 |
| . | 區分文化特性的符號,分隔天數與小時。 不變異格式會使用句號 (“.”) 字元。 |
| hh | 小時,範圍從 0 到 23。 |
| : | 因應文化的時間分隔符號。 不變異格式會使用冒號 (“:”) 字元。 |
| 毫米 | 分鐘,範圍從 0 到 59。 |
| ss | 選擇性秒,範圍從 0 到 59。 |
| . | 能夠反映文化特性的符號,用來分隔秒與秒的小數部分。 不變異格式會使用句號 (“.”) 字元。 |
| ff | 可選的小數秒數,由一到七位數字組成。 |
s 的元件必須共同指定大於或等於 TimeSpan.MinValue 且小於或等於 TimeSpan.MaxValue的時間間隔。
Parse(String) 方法會嘗試使用符合目前文化的每種格式來剖析 s。
TryParse(String, IFormatProvider, TimeSpan) 方法
TryParse(String, IFormatProvider, TimeSpan) 方法就像 Parse(String, IFormatProvider) 方法,不同之處在於,如果轉換失敗,則不會擲回例外狀況。
input 參數包含的時間間隔格式規範:
[ws][-]{ d | d.hh:mm[:ss[.ff]] | hh:mm[:ss[.ff]] }[ws]
方括弧 ([ 和 ]) 中的元素是選擇性的。 必須從大括號({ 與 })括住並以垂直線(|)分隔的選項清單中選取一個。 下表說明每個元素。
| 元素 | Description |
|---|---|
| ws | 選擇性的空格符。 |
| - | 選擇性減號,表示負數 TimeSpan。 |
| d | 天,範圍從 0 到 10675199。 |
| . | 區分文化特性的符號,分隔天數與小時。 不變異格式會使用句號 (“.”) 字元。 |
| hh | 小時,範圍從 0 到 23。 |
| : | 因應文化的時間分隔符號。 不變異格式會使用冒號 (“:”) 字元。 |
| 毫米 | 分鐘,範圍從 0 到 59。 |
| ss | 選擇性秒,範圍從 0 到 59。 |
| . | 能夠反映文化特性的符號,用來分隔秒與秒的小數部分。 不變異格式會使用句號 (“.”) 字元。 |
| ff | 可選的小數秒數,由一到七位數字組成。 |
input 的元件必須共同指定大於或等於 TimeSpan.MinValue 且小於或等於 TimeSpan.MaxValue的時間間隔。
TryParse(String, IFormatProvider, TimeSpan) 方法會嘗試使用 input所指定之文化的各種特定格式來剖析 formatProvider。
formatProvider 參數是 IFormatProvider 實作,可提供傳回字串格式的文化特性特定資訊。
formatProvider 參數可以是下列任一項:
- 代表文化特性的 CultureInfo 物件,其格式慣例將在所傳回的字串中反映出來。 DateTimeFormatInfo 屬性所傳回 CultureInfo.DateTimeFormat 物件會定義傳回字串的格式。
- 定義傳回字串格式的 DateTimeFormatInfo 物件。
- 實作 IFormatProvider 介面的自定義物件。 其 IFormatProvider.GetFormat 方法會傳回提供格式資訊的 DateTimeFormatInfo 物件。
如果 formatProvider 是 null,則會使用與目前文化特性相關聯的 DateTimeFormatInfo 物件。
適用於
TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan)
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
利用指定的文化特定格式資訊,將時間區間的跨度表示轉換為其 TimeSpan 等效值,並回傳一個表示轉換是否成功的值。
public:
static bool TryParse(ReadOnlySpan<char> input, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result) = ISpanParsable<TimeSpan>::TryParse;
public:
static bool TryParse(ReadOnlySpan<char> input, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParse(ReadOnlySpan<char> input, IFormatProvider? formatProvider, out TimeSpan result);
public static bool TryParse(ReadOnlySpan<char> input, IFormatProvider formatProvider, out TimeSpan result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * TimeSpan -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean
參數
- input
- ReadOnlySpan<Char>
一個包含代表要轉換時間區間字元的跨度。
- formatProvider
- IFormatProvider
物件,提供特定文化特性的格式資訊。
傳回
true若input成功轉換;否則,。 false 若false參數為inputnull或,格式無效,代表時間區間小於 Empty 或大於 TimeSpan.MaxValue,或至少有一個天、小時、分鐘或秒的成分超出有效範圍,則此操作會回傳。
適用於
TryParse(String, IFormatProvider, TimeSpan)
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
利用指定的文化特定格式資訊,將時間區間的字串表示轉換為 TimeSpan 等價值,並回傳一個表示轉換是否成功的值。
public:
static bool TryParse(System::String ^ input, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public:
static bool TryParse(System::String ^ input, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result) = IParsable<TimeSpan>::TryParse;
public static bool TryParse(string input, IFormatProvider formatProvider, out TimeSpan result);
public static bool TryParse(string? input, IFormatProvider? formatProvider, out TimeSpan result);
static member TryParse : string * IFormatProvider * TimeSpan -> bool
Public Shared Function TryParse (input As String, formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean
參數
- input
- String
字串,指定要轉換的時間間隔。
- formatProvider
- IFormatProvider
物件,提供特定文化特性的格式資訊。
傳回
true若input成功轉換;否則,。 false 若false參數為inputnull或,格式無效,代表時間區間小於 Empty 或大於 TimeSpan.MaxValue,或至少有一個天、小時、分鐘或秒的成分超出有效範圍,則此操作會回傳。
範例
以下範例定義了一個物件陣 CultureInfo 列,並利用每個物件 TryParse(String, IFormatProvider, TimeSpan) 呼叫方法來解析字串陣列中的元素。 此範例說明特定文化特性的慣例如何影響格式化作業。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] values = { "6", "6:12", "6:12:14", "6:12:14:45",
"6.12:14:45", "6:12:14:45.3448",
"6:12:14:45,3448", "6:34:14:45" };
CultureInfo[] cultures = { new CultureInfo("en-US"),
new CultureInfo("ru-RU"),
CultureInfo.InvariantCulture };
string header = String.Format("{0,-17}", "String");
foreach (CultureInfo culture in cultures)
header += culture.Equals(CultureInfo.InvariantCulture) ?
String.Format("{0,20}", "Invariant") :
String.Format("{0,20}", culture.Name);
Console.WriteLine(header);
Console.WriteLine();
foreach (string value in values)
{
Console.Write("{0,-17}", value);
foreach (CultureInfo culture in cultures)
{
TimeSpan interval = new TimeSpan();
if (TimeSpan.TryParse(value, culture, out interval))
Console.Write("{0,20}", interval.ToString("c"));
else
Console.Write("{0,20}", "Unable to Parse");
}
Console.WriteLine();
}
}
}
// The example displays the following output:
// String en-US ru-RU Invariant
//
// 6 6.00:00:00 6.00:00:00 6.00:00:00
// 6:12 06:12:00 06:12:00 06:12:00
// 6:12:14 06:12:14 06:12:14 06:12:14
// 6:12:14:45 6.12:14:45 6.12:14:45 6.12:14:45
// 6.12:14:45 6.12:14:45 6.12:14:45 6.12:14:45
// 6:12:14:45.3448 6.12:14:45.3448000 Unable to Parse 6.12:14:45.3448000
// 6:12:14:45,3448 Unable to Parse 6.12:14:45.3448000 Unable to Parse
// 6:34:14:45 Unable to Parse Unable to Parse Unable to Parse
open System
open System.Globalization
let values =
[| "6"; "6:12"; "6:12:14"; "6:12:14:45"
"6.12:14:45"; "6:12:14:45.3448"
"6:12:14:45,3448"; "6:34:14:45" |]
let cultures =
[| CultureInfo "en-US"
CultureInfo "ru-RU"
CultureInfo.InvariantCulture |]
let mutable header = String.Format("{0,-17}", "String")
for culture in cultures do
header <-
if culture.Equals CultureInfo.InvariantCulture then
String.Format("{0,20}", "Invariant")
else
String.Format("{0,20}", culture.Name)
printfn $"{header}\n"
for value in values do
printf $"{value,-17}"
for culture in cultures do
match TimeSpan.TryParse(value, culture) with
| true, interval ->
printfn $"{interval,20:c}"
| _ ->
printfn "%20s" "Unable to Parse"
// The example displays the following output:
// String en-US ru-RU Invariant
//
// 6 6.00:00:00 6.00:00:00 6.00:00:00
// 6:12 06:12:00 06:12:00 06:12:00
// 6:12:14 06:12:14 06:12:14 06:12:14
// 6:12:14:45 6.12:14:45 6.12:14:45 6.12:14:45
// 6.12:14:45 6.12:14:45 6.12:14:45 6.12:14:45
// 6:12:14:45.3448 6.12:14:45.3448000 Unable to Parse 6.12:14:45.3448000
// 6:12:14:45,3448 Unable to Parse 6.12:14:45.3448000 Unable to Parse
// 6:34:14:45 Unable to Parse Unable to Parse Unable to Parse
Dim values() As String = {"6", "6:12", "6:12:14", "6:12:14:45",
"6.12:14:45", "6:12:14:45.3448",
"6:12:14:45,3448", "6:34:14:45"}
Dim cultures() As CultureInfo = {New CultureInfo("en-US"),
New CultureInfo("ru-RU"),
CultureInfo.InvariantCulture}
Dim header As String = String.Format("{0,-17}", "String")
For Each culture As CultureInfo In cultures
header += If(culture.Equals(CultureInfo.InvariantCulture),
String.Format("{0,20}", "Invariant"),
String.Format("{0,20}", culture.Name))
Next
Console.WriteLine(header)
Console.WriteLine()
For Each value As String In values
Console.Write("{0,-17}", value)
For Each culture As CultureInfo In cultures
Dim interval As New TimeSpan()
If TimeSpan.TryParse(value, culture, interval) Then
Console.Write("{0,20}", interval.ToString("c"))
Else
Console.Write("{0,20}", "Unable to Parse")
End If
Next
Console.WriteLine()
Next
' The example displays the following output:
' String en-US ru-RU Invariant
'
' 6 6.00:00:00 6.00:00:00 6.00:00:00
' 6:12 06:12:00 06:12:00 06:12:00
' 6:12:14 06:12:14 06:12:14 06:12:14
' 6:12:14:45 6.12:14:45 6.12:14:45 6.12:14:45
' 6.12:14:45 6.12:14:45 6.12:14:45 6.12:14:45
' 6:12:14:45.3448 6.12:14:45.3448000 Unable to Parse 6.12:14:45.3448000
' 6:12:14:45,3448 Unable to Parse 6.12:14:45.3448000 Unable to Parse
' 6:34:14:45 Unable to Parse Unable to Parse Unable to Parse
備註
TryParse(System.String,System.TimeSpan@) 方法
TimeSpan.TryParse(String, TimeSpan) 方法就像 TimeSpan.Parse(String) 方法,不同之處在於,如果轉換失敗,則不會擲回例外狀況。
s 參數包含的時間間隔格式規範:
[ws][-]{ d | d.hh:mm[:ss[.ff]] | hh:mm[:ss[.ff]] }[ws]
方括弧 ([ 和 ]) 中的元素是選擇性的。 必須從大括號({ 與 })括住並以垂直線(|)分隔的選項清單中選取一個。 下表說明每個元素。
| 元素 | Description |
|---|---|
| ws | 選擇性的空格符。 |
| - | 選擇性減號,表示負數 TimeSpan。 |
| d | 天,範圍從 0 到 10675199。 |
| . | 區分文化特性的符號,分隔天數與小時。 不變異格式會使用句號 (“.”) 字元。 |
| hh | 小時,範圍從 0 到 23。 |
| : | 因應文化的時間分隔符號。 不變異格式會使用冒號 (“:”) 字元。 |
| 毫米 | 分鐘,範圍從 0 到 59。 |
| ss | 選擇性秒,範圍從 0 到 59。 |
| . | 能夠反映文化特性的符號,用來分隔秒與秒的小數部分。 不變異格式會使用句號 (“.”) 字元。 |
| ff | 可選的小數秒數,由一到七位數字組成。 |
s 的元件必須共同指定大於或等於 TimeSpan.MinValue 且小於或等於 TimeSpan.MaxValue的時間間隔。
Parse(String) 方法會嘗試使用符合目前文化的每種格式來剖析 s。
TryParse(String, IFormatProvider, TimeSpan) 方法
TryParse(String, IFormatProvider, TimeSpan) 方法就像 Parse(String, IFormatProvider) 方法,不同之處在於,如果轉換失敗,則不會擲回例外狀況。
input 參數包含的時間間隔格式規範:
[ws][-]{ d | d.hh:mm[:ss[.ff]] | hh:mm[:ss[.ff]] }[ws]
方括弧 ([ 和 ]) 中的元素是選擇性的。 必須從大括號({ 與 })括住並以垂直線(|)分隔的選項清單中選取一個。 下表說明每個元素。
| 元素 | Description |
|---|---|
| ws | 選擇性的空格符。 |
| - | 選擇性減號,表示負數 TimeSpan。 |
| d | 天,範圍從 0 到 10675199。 |
| . | 區分文化特性的符號,分隔天數與小時。 不變異格式會使用句號 (“.”) 字元。 |
| hh | 小時,範圍從 0 到 23。 |
| : | 因應文化的時間分隔符號。 不變異格式會使用冒號 (“:”) 字元。 |
| 毫米 | 分鐘,範圍從 0 到 59。 |
| ss | 選擇性秒,範圍從 0 到 59。 |
| . | 能夠反映文化特性的符號,用來分隔秒與秒的小數部分。 不變異格式會使用句號 (“.”) 字元。 |
| ff | 可選的小數秒數,由一到七位數字組成。 |
input 的元件必須共同指定大於或等於 TimeSpan.MinValue 且小於或等於 TimeSpan.MaxValue的時間間隔。
TryParse(String, IFormatProvider, TimeSpan) 方法會嘗試使用 input所指定之文化的各種特定格式來剖析 formatProvider。
formatProvider 參數是 IFormatProvider 實作,可提供傳回字串格式的文化特性特定資訊。
formatProvider 參數可以是下列任一項:
- 代表文化特性的 CultureInfo 物件,其格式慣例將在所傳回的字串中反映出來。 DateTimeFormatInfo 屬性所傳回 CultureInfo.DateTimeFormat 物件會定義傳回字串的格式。
- 定義傳回字串格式的 DateTimeFormatInfo 物件。
- 實作 IFormatProvider 介面的自定義物件。 其 IFormatProvider.GetFormat 方法會傳回提供格式資訊的 DateTimeFormatInfo 物件。
如果 formatProvider 是 null,則會使用與目前文化特性相關聯的 DateTimeFormatInfo 物件。