TimeSpan.Parse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將時間間隔的字串表示,轉換成與其相等的 TimeSpan。
多載
Parse(String) |
將時間間隔的字串表示,轉換成與其相等的 TimeSpan。 |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
使用指定的特定文化特性格式資訊,將時間間隔的範圍表示轉換為其對等的 TimeSpan。 |
Parse(String, IFormatProvider) |
使用指定的特定文化特性格式資訊,將時間間隔的字串表示轉換為其相等的 TimeSpan。 |
Parse(String)
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
將時間間隔的字串表示,轉換成與其相等的 TimeSpan。
public:
static TimeSpan Parse(System::String ^ s);
public static TimeSpan Parse (string s);
static member Parse : string -> TimeSpan
Public Shared Function Parse (s As String) As TimeSpan
參數
- s
- String
字串,指定要轉換的時間間隔。
傳回
時間間隔,對應至 s
。
例外狀況
s
為 null
。
s
具有無效的格式。
範例
下列範例會使用 方法, Parse 將字串陣列 TimeSpan 中的每個項目轉換成值。 它會將目前的系統文化特性變更為克羅埃西亞文 - 克羅埃西亞 (“hr-HR”) 和英文 - 美國 (“en-US”) ,以說明目前系統文化特性如何影響剖析作業。
using System;
using System.Globalization;
using System.Threading;
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" };
string[] cultureNames = { "hr-HR", "en-US"};
// Change the current culture.
foreach (string cultureName in cultureNames)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.Name);
foreach (string value in values)
{
try {
TimeSpan ts = TimeSpan.Parse(value);
Console.WriteLine("{0} --> {1}", value, ts.ToString("c"));
}
catch (FormatException) {
Console.WriteLine("{0}: Bad Format", value);
}
catch (OverflowException) {
Console.WriteLine("{0}: Overflow", value);
}
}
Console.WriteLine();
}
}
}
// The example displays the following output:
// Current Culture: hr-HR
// 6 --> 6.00:00:00
// 6:12 --> 06:12:00
// 6: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.3448: Bad Format
// 6:12:14:45,3448 --> 6.12:14:45.3448000
// 6:34:14:45: Overflow
//
// Current Culture: en-US
// 6 --> 6.00:00:00
// 6:12 --> 06:12:00
// 6: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.3448 --> 6.12:14:45.3448000
// 6:12:14:45,3448: Bad Format
// 6:34:14:45: Overflow
open System
open System.Globalization
open System.Threading
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 cultureNames = [| "hr-HR"; "en-US" |]
// Change the current culture.
for cultureName in cultureNames do
Thread.CurrentThread.CurrentCulture <- CultureInfo cultureName
printfn $"Current Culture: {Thread.CurrentThread.CurrentCulture.Name}"
for value in values do
try
let ts = TimeSpan.Parse value
printfn $"{value} --> {ts:c}"
with
| :? FormatException ->
printfn $"{value}: Bad Format"
| :? OverflowException ->
printfn $"{value}: Overflow"
printfn ""
// The example displays the following output:
// Current Culture: hr-HR
// 6 --> 6.00:00:00
// 6:12 --> 06:12:00
// 6: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.3448: Bad Format
// 6:12:14:45,3448 --> 6.12:14:45.3448000
// 6:34:14:45: Overflow
//
// Current Culture: en-US
// 6 --> 6.00:00:00
// 6:12 --> 06:12:00
// 6: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.3448 --> 6.12:14:45.3448000
// 6:12:14:45,3448: Bad Format
// 6:34:14:45: Overflow
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
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 cultureNames() As String = { "hr-HR", "en-US"}
' Change the current culture.
For Each cultureName As String In cultureNames
Thread.CurrentThread.CurrentCulture = New CultureInfo(cultureName)
Console.WriteLine("Current Culture: {0}",
Thread.CurrentThread.CurrentCulture.Name)
For Each value As String In values
Try
Dim ts As TimeSpan = TimeSpan.Parse(value)
Console.WriteLine("{0} --> {1}", value, ts.ToString("c"))
Catch e As FormatException
Console.WriteLine("{0}: Bad Format", value)
Catch e As OverflowException
Console.WriteLine("{0}: Overflow", value)
End Try
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Current Culture: hr-HR
' 6 --> 6.00:00:00
' 6:12 --> 06:12:00
' 6: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.3448: Bad Format
' 6:12:14:45,3448 --> 6.12:14:45.3448000
' 6:34:14:45: Overflow
'
' Current Culture: en-US
' 6 --> 6.00:00:00
' 6:12 --> 06:12:00
' 6: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.3448 --> 6.12:14:45.3448000
' 6:12:14:45,3448: Bad Format
' 6:34:14:45: Overflow
備註
如需此 API 的詳細資訊,請參閱 TimeSpan.Parse 的補充 API 備註。
適用於
Parse(ReadOnlySpan<Char>, IFormatProvider)
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
使用指定的特定文化特性格式資訊,將時間間隔的範圍表示轉換為其對等的 TimeSpan。
public static TimeSpan Parse (ReadOnlySpan<char> input, IFormatProvider? formatProvider = default);
public static TimeSpan Parse (ReadOnlySpan<char> input, IFormatProvider formatProvider = default);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> TimeSpan
Public Shared Function Parse (input As ReadOnlySpan(Of Char), Optional formatProvider As IFormatProvider = Nothing) As TimeSpan
參數
- input
- ReadOnlySpan<Char>
範圍,其包含代表要轉換時間間隔的字元。
- formatProvider
- IFormatProvider
物件,提供特定文化特性格式資訊。
傳回
時間間隔,對應至 input
,如 formatProvider
所指定。
實作
適用於
Parse(String, IFormatProvider)
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
- 來源:
- TimeSpan.cs
使用指定的特定文化特性格式資訊,將時間間隔的字串表示轉換為其相等的 TimeSpan。
public:
static TimeSpan Parse(System::String ^ input, IFormatProvider ^ formatProvider);
public:
static TimeSpan Parse(System::String ^ input, IFormatProvider ^ formatProvider) = IParsable<TimeSpan>::Parse;
public static TimeSpan Parse (string input, IFormatProvider formatProvider);
public static TimeSpan Parse (string input, IFormatProvider? formatProvider);
static member Parse : string * IFormatProvider -> TimeSpan
Public Shared Function Parse (input As String, formatProvider As IFormatProvider) As TimeSpan
參數
- input
- String
字串,指定要轉換的時間間隔。
- formatProvider
- IFormatProvider
物件,提供特定文化特性格式資訊。
傳回
時間間隔,對應至 input
,如 formatProvider
所指定。
實作
例外狀況
input
為 null
。
input
具有無效的格式。
範例
下列範例會定義 物件的陣列 CultureInfo ,並使用呼叫 Parse(String, IFormatProvider) 方法中的每個物件來剖析字串數位列中的專案。 此範例說明特定文化特性的慣例如何影響格式化作業。
using System;
using System.Globalization;
using System.Text.RegularExpressions;
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)
{
try {
TimeSpan ts = TimeSpan.Parse(value, culture);
Console.Write("{0,20}", ts.ToString("c"));
}
catch (FormatException) {
Console.Write("{0,20}", "Bad Format");
}
catch (OverflowException) {
Console.Write("{0,20}", "Overflow");
}
}
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 Bad Format 6.12:14:45.3448000
// 6:12:14:45,3448 Bad Format 6.12:14:45.3448000 Bad Format
// 6:34:14:45 Overflow Overflow Overflow
open System
open System.Globalization
open System.Text.RegularExpressions
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",-17}"""
for culture in cultures do
header <- header +
if culture.Equals CultureInfo.InvariantCulture then
$"""{"Invariant",20}"""
else
$"{culture.Name,20}"
printfn $"{header}\m"
for value in values do
printf $"{value,-17}"
for culture in cultures do
try
let ts = TimeSpan.Parse(value, culture)
printf $"{ts,20:c}"
with
| :? FormatException ->
printf $"""{"Bad Format",20}"""
| :? OverflowException ->
printf $"""{"Overflow",20}"""
printfn ""
// 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 Bad Format 6.12:14:45.3448000
// 6:12:14:45,3448 Bad Format 6.12:14:45.3448000 Bad Format
// 6:34:14:45 Overflow Overflow Overflow
Imports System.Globalization
Imports System.Threading
Module Example
Public Sub Main()
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
Try
Dim ts As TimeSpan = TimeSpan.Parse(value, culture)
Console.Write("{0,20}", ts.ToString("c"))
Catch e As FormatException
Console.Write("{0,20}", "Bad Format")
Catch e As OverflowException
Console.Write("{0,20}", "Overflow")
End Try
Next
Console.WriteLine()
Next
End Sub
End Module
' 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 Bad Format 6.12:14:45.3448000
' 6:12:14:45,3448 Bad Format 6.12:14:45.3448000 Bad Format
' 6:34:14:45 Overflow Overflow Overflow
備註
這個方法會嘗試使用 所formatProvider
指定文化特性的每個特定文化特性格式來剖input
析 。
參數 formatProvider
是實作 IFormatProvider ,可提供傳回字串格式的文化特性特定資訊。 參數 formatProvider
可以是下列任一項:
- CultureInfo物件,表示格式設定慣例要反映在傳回字串中的文化特性。 屬性 DateTimeFormatInfo 所傳 CultureInfo.DateTimeFormat 回的物件會定義傳回字串的格式設定。
- DateTimeFormatInfo對象,定義傳回字串的格式設定。
- 實作 介面的 IFormatProvider 自定義物件。 其 IFormatProvider.GetFormat 方法會傳回提供 DateTimeFormatInfo 格式化信息的物件。
如果 為 formatProvider
null
,則會 DateTimeFormatInfo 使用與目前文化特性相關聯的物件。
如需此 API 的詳細資訊,請參閱 System.TimeSpan.Parse 的補充 API 備註。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應