使用英语阅读

通过


TimeSpan.Parse 方法

定义

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

重载

Parse(String, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, IFormatProvider)

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

Parse(String)

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

Parse(String, IFormatProvider)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

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

public static TimeSpan Parse (string input, IFormatProvider formatProvider);
public static TimeSpan Parse (string input, IFormatProvider? formatProvider);

参数

input
String

一个字符串,指定要转换的时间间隔。

formatProvider
IFormatProvider

提供区域性特定的格式设置信息的对象。

返回

对应于由 formatProvider指定的 input的时间间隔。

实现

例外

input null

input 格式无效。

input 表示小于 TimeSpan.MinValue 或大于 TimeSpan.MaxValue的数字。

-或-

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

注解

此方法尝试使用 formatProvider指定的区域性的每个区域性特定格式分析 input

formatProvider 参数是一个 IFormatProvider 实现,提供有关返回字符串格式的区域性特定信息。 formatProvider 参数可以是以下任一参数:

如果 formatProvidernull,则使用与当前区域性关联的 DateTimeFormatInfo 对象。

有关此 API 的详细信息,请参阅 System.TimeSpan.Parse的补充 API 备注。

适用于

Parse(ReadOnlySpan<Char>, IFormatProvider)

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);

参数

input
ReadOnlySpan<Char>

一个范围,包含表示要转换的时间间隔的字符。

formatProvider
IFormatProvider

提供区域性特定的格式设置信息的对象。

返回

对应于由 formatProvider指定的 input的时间间隔。

实现

适用于

Parse(String)

Source:
TimeSpan.cs
Source:
TimeSpan.cs
Source:
TimeSpan.cs

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

public static TimeSpan Parse (string s);

参数

s
String

一个字符串,指定要转换的时间间隔。

返回

对应于 s的时间间隔。

例外

s 格式无效。

s 表示小于 TimeSpan.MinValue 或大于 TimeSpan.MaxValue的数字。

-或-

至少有一天、小时、分钟或秒组件超出其有效范围。

示例

以下示例使用 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

注解

有关此 API 的详细信息,请参阅 TimeSpan.Parse的补充 API 备注。

适用于