語言

TimeSpan.Parse 方法

定義

將時間區間的字串表示轉換為其 TimeSpan 等價值。

多載

名稱 Description
Parse(ReadOnlySpan<Char>, IFormatProvider)

利用指定的文化特定格式資訊,將時間區間的跨度表示轉換為 TimeSpan 其等效值。

Parse(String, IFormatProvider)

利用指定的文化特定格式資訊,將時間區間的字串表示轉換為其 TimeSpan 等價值。

Parse(String)

將時間區間的字串表示轉換為其 TimeSpan 等價值。

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
TimeSpan.cs
來源:
TimeSpan.cs
來源:
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.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 格式無效。

input 代表小於 TimeSpan.MinValue 或大於 TimeSpan.MaxValue 的數字。

-或-

至少有一天、小時、分鐘或秒的 input 成分超出其有效範圍。

範例

以下範例定義了一個物件陣 CultureInfo 列,並利用每個物件 Parse(String, IFormatProvider) 呼叫方法來解析字串陣列中的元素。 此範例說明特定文化特性的慣例如何影響格式化作業。

using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example2
{
   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
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

' 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

備註

此方法嘗試透過使用由 input所指定的文化的每個特定格式來解析formatProvider。

formatProvider 參數是 IFormatProvider 實作,可提供傳回字串格式的文化特性特定資訊。 formatProvider 參數可以是下列任一項:

如果 formatProvider 是 null,則會使用與目前文化特性相關聯的 DateTimeFormatInfo 物件。

Parse 方法的輸入字串包含一個以格式表示的時間間隔規格:

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

方括弧中的元素([ 和 ])是選擇性的。 必須從大括弧({ 和 })括住的選項清單中選取一個,並以垂直線分隔(|)。 下表說明每個元素。

元素 Description
ws 選擇性的空格符。
- 選擇性減號,表示負數 TimeSpan。
d 天,範圍從 0 到 10675199。
. 區分文化特性的符號,分隔天數與小時。 不變異格式會使用句號 (“.”) 字元。
hh 小時,範圍從 0 到 23。
: 因應文化的時間分隔符號。 不變異格式會使用冒號 (“:”) 字元。
毫米 分鐘,範圍從 0 到 59。
ss 選擇性秒,範圍從 0 到 59。
. 能夠反映文化特性的符號,用來分隔秒與秒的小數部分。 不變異格式會使用句號 (“.”) 字元。
ff 可選的小數秒數,由一到七位數字組成。

如果輸入字串不是僅一天值,它必須包含小時和分鐘元件;其他元件是選擇性的。 如果存在,則每個時間元件的值都必須落在指定的範圍內。 例如,hh的值,其小時部分必須介於 0 到 23 之間。 因此,將 “23:00:00” 傳遞給 Parse 方法會傳回 23 小時的時間間隔。 另一方面,傳遞 「24:00:00」 會傳回 24 天的時間間隔。 因為 「24」 超出時數元件的範圍,所以它會解譯為 days 元件。

輸入字串的元件必須共同指定大於或等於 TimeSpan.MinValue 且小於或等於 TimeSpan.MaxValue的時間間隔。

Parse(String) 方法會嘗試使用目前文化的各種格式來剖析輸入字串。

適用於

Parse(String)

來源:
TimeSpan.cs
來源:
TimeSpan.cs
來源:
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 格式無效。

s 代表小於 TimeSpan.MinValue 或大於 TimeSpan.MaxValue 的數字。

-或-

至少有一個天數、小時、分鐘或秒元件超出其有效範圍。

範例

以下範例使用此 Parse 方法將字串陣列中的每個元素轉換為一個 TimeSpan 值。 它將現有系統文化改為克羅埃西亞語 - 克羅埃西亞語(「hr-HR」)和英語 - 美國(「en-US」),以說明現行系統文化如何影響解析操作。

using System;
using System.Globalization;
using System.Threading;

public class Example1
{
   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
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

' 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

備註

Parse 方法的輸入字串包含一個以格式表示的時間間隔規格:

[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]

方括弧中的元素([ 和 ])是選擇性的。 必須從大括弧({ 和 })括住的選項清單中選取一個,並以垂直線分隔(|)。 下表說明每個元素。

元素 Description
ws 選擇性的空格符。
- 選擇性減號,表示負數 TimeSpan。
d 天,範圍從 0 到 10675199。
. 區分文化特性的符號,分隔天數與小時。 不變異格式會使用句號 (“.”) 字元。
hh 小時,範圍從 0 到 23。
: 因應文化的時間分隔符號。 不變異格式會使用冒號 (“:”) 字元。
毫米 分鐘,範圍從 0 到 59。
ss 選擇性秒,範圍從 0 到 59。
. 能夠反映文化特性的符號,用來分隔秒與秒的小數部分。 不變異格式會使用句號 (“.”) 字元。
ff 可選的小數秒數,由一到七位數字組成。

如果輸入字串不是僅一天值,它必須包含小時和分鐘元件;其他元件是選擇性的。 如果存在,則每個時間元件的值都必須落在指定的範圍內。 例如,hh的值,其小時部分必須介於 0 到 23 之間。 因此,將 “23:00:00” 傳遞給 Parse 方法會傳回 23 小時的時間間隔。 另一方面,傳遞 「24:00:00」 會傳回 24 天的時間間隔。 因為 「24」 超出時數元件的範圍,所以它會解譯為 days 元件。

輸入字串的元件必須共同指定大於或等於 TimeSpan.MinValue 且小於或等於 TimeSpan.MaxValue的時間間隔。

Parse(String) 方法會嘗試使用目前文化的各種格式來剖析輸入字串。

適用於