语言

TimeSpan.Parse 方法

定义

将时间间隔的字符串表示形式转换为其 TimeSpan 等效的表示形式。

重载

名称 说明
Parse(ReadOnlySpan<Char>, IFormatProvider)

使用指定的区域性特定格式信息将时间间隔的跨度表示形式转换为其等效 TimeSpan。

Parse(String, IFormatProvider)

使用指定的区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效 TimeSpan。

Parse(String)

将时间间隔的字符串表示形式转换为其 TimeSpan 等效的表示形式。

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
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)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
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”超出了小时分量的范围,因此将其解释为天分量。

输入字符串的组件必须统一指定大于或等于 TimeSpan.MinValue 且小于或等于的 TimeSpan.MaxValue时间间隔。

Parse(String) 方法会尝试使用当前区域性的每个区域性特定格式分析输入字符串。

适用于

Parse(String)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
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”超出了小时分量的范围,因此将其解释为天分量。

输入字符串的组件必须统一指定大于或等于 TimeSpan.MinValue 且小于或等于的 TimeSpan.MaxValue时间间隔。

Parse(String) 方法会尝试使用当前区域性的每个区域性特定格式分析输入字符串。

适用于