TimeSpan.TryParse TimeSpan.TryParse TimeSpan.TryParse TimeSpan.TryParse Method

定义

将时间间隔的指定字符串表示形式转换为其等效的 TimeSpan,并返回一个指示转换是否成功的值。Converts the specified string representation of a time interval to its TimeSpan equivalent and returns a value that indicates whether the conversion succeeded.

重载

TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan) TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan) TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan) TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan)
TryParse(ReadOnlySpan<Char>, TimeSpan) TryParse(ReadOnlySpan<Char>, TimeSpan) TryParse(ReadOnlySpan<Char>, TimeSpan) TryParse(ReadOnlySpan<Char>, TimeSpan)
TryParse(String, TimeSpan) TryParse(String, TimeSpan) TryParse(String, TimeSpan) TryParse(String, TimeSpan)

将时间间隔的字符串表示形式转换为其等效的 TimeSpan,并返回一个指示转换是否成功的值。Converts the string representation of a time interval to its TimeSpan equivalent and returns a value that indicates whether the conversion succeeded.

TryParse(String, IFormatProvider, TimeSpan) TryParse(String, IFormatProvider, TimeSpan) TryParse(String, IFormatProvider, TimeSpan) TryParse(String, IFormatProvider, TimeSpan)

使用指定的区域性特定格式设置信息,将时间间隔的字符串表示形式转换为其等效的 TimeSpan,并返回一个指示转换是否成功的值。Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific formatting information, and returns a value that indicates whether the conversion succeeded.

TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan) TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan) TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan) TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan)

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);
static member TryParse : ReadOnlySpan<char> * IFormatProvider *  -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean

参数

返回

TryParse(ReadOnlySpan<Char>, TimeSpan) TryParse(ReadOnlySpan<Char>, TimeSpan) TryParse(ReadOnlySpan<Char>, TimeSpan) TryParse(ReadOnlySpan<Char>, 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> *  -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As TimeSpan) As Boolean

参数

返回

TryParse(String, TimeSpan) TryParse(String, TimeSpan) TryParse(String, TimeSpan) TryParse(String, TimeSpan)

将时间间隔的字符串表示形式转换为其等效的 TimeSpan,并返回一个指示转换是否成功的值。Converts the string representation of a time interval to its TimeSpan equivalent and returns a value that indicates whether the conversion succeeded.

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParse (string s, out TimeSpan result);
static member TryParse : string *  -> bool
Public Shared Function TryParse (s As String, ByRef result As TimeSpan) As Boolean

参数

s
String String String String

一个字符串,用于指定进行转换的时间间隔。A string that specifies the time interval to convert.

result
TimeSpan TimeSpan TimeSpan TimeSpan

此方法返回时,包含表示由 s 指定的时间间隔的对象;或者如果转换失败,则包含 ZeroWhen this method returns, contains an object that represents the time interval specified by s, or Zero if the conversion failed. 此参数未经初始化即被传递。This parameter is passed uninitialized.

返回

如果 true 成功转换,则为 s;否则为 falsetrue if s was converted successfully; otherwise, false. 如果 s 参数为 nullEmpty,格式无效,表示的时间间隔小于 MinValue 或大于 MaxValue,或者天、小时、分钟或秒分量中至少有一个超出其有效范围,则此运算返回 falseThis operation returns false if the s parameter is null or Empty, has an invalid format, represents a time interval that is less than MinValue or greater than MaxValue, or has at least one days, hours, minutes, or seconds component outside its valid range.

示例

下面的示例使用TryParse方法从有效TimeSpanTimeSpan字符串创建对象, 并指示当分析操作因为时间跨度字符串无效而失败的时间。The following example uses the TryParse method to create TimeSpan objects from valid TimeSpan strings and to indicate when the parse operation has failed because the time span string is invalid.

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

注解

方法与TimeSpan.Parse(String)方法类似, 不同之处在于它不会在转换失败时引发异常。 TryParseThe TryParse method is like the TimeSpan.Parse(String) method, except that it does not throw an exception if the conversion fails.

s参数包含以下格式的时间间隔规范:The s parameter contains a time interval specification in the form:

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

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 需要使用括在大括号 ({和}) 中的替代项列表进行一项选择,|并用竖线 () 分隔。One selection from the list of alternatives enclosed in braces ({ and }) and separated by vertical bars (|) is required. 下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space.
- 可选的负号, 指示负TimeSpan号。An optional minus sign, which indicates a negative TimeSpan.
dd 天, 范围介于0到10675199之间。Days, ranging from 0 to 10675199.
.. 一种区分区域性的符号, 用于将天与小时隔开。A culture-sensitive symbol that separates days from hours. 固定格式使用句点 (".") 字符。The invariant format uses a period (".") character.
hhhh 小时, 范围为0到23。Hours, ranging from 0 to 23.
:: 区分区域性的时间分隔符。The culture-sensitive time separator symbol. 固定格式使用冒号 (":") 字符。The invariant format uses a colon (":") character.
mmmm 分钟, 范围为0到59。Minutes, ranging from 0 to 59.
ssss 可选秒, 范围介于0到59之间。Optional seconds, ranging from 0 to 59.
.. 区分区域性的符号, 将秒与秒的小数部分分隔开来。A culture-sensitive symbol that separates seconds from fractions of a second. 固定格式使用句点 (".") 字符。The invariant format uses a period (".") character.
ffff 可选的秒的小数部分, 由1到7个十进制数字组成。Optional fractional seconds, consisting of one to seven decimal digits.

s组件必须共同指定一个大于或TimeSpan.MinValue等于且TimeSpan.MaxValue小于或等于的时间间隔。The components of s must collectively specify a time interval that is greater than or equal to TimeSpan.MinValue and less than or equal to TimeSpan.MaxValue.

Parse(String)方法尝试使用当前s区域性的每个特定于区域性的格式来进行分析。The Parse(String) method tries to parse s by using each of the culture-specific formats for the current culture.

调用方说明Notes to callers

在某些情况下, 当要分析的字符串中的时间间隔组件包含的位数超过7个时, .NET Framework 3.5 及更早版本中成功的分析操作可能会失败OverflowException , 并将在 .NET Framework 4 和更高版本中引发。In some cases, when a time interval component in the string to be parsed contains more than seven digits, parsing operations that succeed in the .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in the .NET Framework 4 and later versions. 下面的示例演示了这种情况。The following example illustrates this scenario.

string value = "000000006";
TimeSpan interval;
if (TimeSpan.TryParse(value, out interval))
   Console.WriteLine("{0} --> {1}", value, interval);
else
   Console.WriteLine("Unable to parse '{0}'", value);
// Output from .NET Framework 3.5 and earlier versions:
//       000000006 --> 6.00:00:00
// Output from .NET Framework 4:
//       Unable to parse //000000006//
Dim value As String = "000000006"
Dim interval As TimeSpan
If TimeSpan.TryParse(value, interval) Then
   Console.WriteLine("{0} --> {1}", value, interval)
Else
   Console.WriteLine("Unable to parse '{0}'", value)
End If
' Output from .NET Framework 3.5 and earlier versions:
'       000000006 --> 6.00:00:00
' Output from .NET Framework 4:
'       Unable to parse '000000006'

TryParse(String, IFormatProvider, TimeSpan) TryParse(String, IFormatProvider, TimeSpan) TryParse(String, IFormatProvider, TimeSpan) TryParse(String, IFormatProvider, TimeSpan)

使用指定的区域性特定格式设置信息,将时间间隔的字符串表示形式转换为其等效的 TimeSpan,并返回一个指示转换是否成功的值。Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific formatting information, and returns a value that indicates whether the conversion succeeded.

public:
 static bool TryParse(System::String ^ input, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParse (string input, IFormatProvider formatProvider, out TimeSpan result);
static member TryParse : string * IFormatProvider *  -> bool
Public Shared Function TryParse (input As String, formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean

参数

input
String String String String

一个字符串,用于指定进行转换的时间间隔。A string that specifies the time interval to convert.

formatProvider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.

result
TimeSpan TimeSpan TimeSpan TimeSpan

此方法返回时,包含表示由 input 指定的时间间隔的对象;或者如果转换失败,则包含 ZeroWhen this method returns, contains an object that represents the time interval specified by input, or Zero if the conversion failed. 此参数未经初始化即被传递。This parameter is passed uninitialized.

返回

如果 true 成功转换,则为 input;否则为 falsetrue if input was converted successfully; otherwise, false. 如果 input 参数为 nullEmpty,格式无效,表示的时间间隔小于 MinValue 或大于 MaxValue,或者天、小时、分钟或秒分量中至少有一个超出其有效范围,则此运算返回 falseThis operation returns false if the input parameter is null or Empty, has an invalid format, represents a time interval that is less than MinValue or greater than MaxValue, or has at least one days, hours, minutes, or seconds component outside its valid range.

示例

下面的示例定义对象的CultureInfo数组, 并在对方法的TryParse(String, IFormatProvider, TimeSpan)调用中使用每个对象来分析字符串数组中的元素。The following example defines an array of CultureInfo objects, and uses each object in calls to the TryParse(String, IFormatProvider, TimeSpan) method to parse the elements in a string array. 该示例演示特定区域性的约定如何影响格式设置操作。The example illustrates how the conventions of a specific culture influence the formatting operation.

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
Imports System.Globalization

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

注解

方法与Parse(String, IFormatProvider)方法类似, 不同之处在于它不会在转换失败时引发异常。 TryParse(String, IFormatProvider, TimeSpan)The TryParse(String, IFormatProvider, TimeSpan) method is like the Parse(String, IFormatProvider) method, except that it does not throw an exception if the conversion fails.

input参数包含以下格式的时间间隔规范:The input parameter contains a time interval specification in the form:

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

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 需要使用括在大括号 ({和}) 中的替代项列表进行一项选择,|并用竖线 () 分隔。One selection from the list of alternatives enclosed in braces ({ and }) and separated by vertical bars (|) is required. 下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space.
- 可选的负号, 指示负TimeSpan号。An optional minus sign, which indicates a negative TimeSpan.
dd 天, 范围介于0到10675199之间。Days, ranging from 0 to 10675199.
.. 一种区分区域性的符号, 用于将天与小时隔开。A culture-sensitive symbol that separates days from hours. 固定格式使用句点 (".") 字符。The invariant format uses a period (".") character.
hhhh 小时, 范围为0到23。Hours, ranging from 0 to 23.
:: 区分区域性的时间分隔符。The culture-sensitive time separator symbol. 固定格式使用冒号 (":") 字符。The invariant format uses a colon (":") character.
mmmm 分钟, 范围为0到59。Minutes, ranging from 0 to 59.
ssss 可选秒, 范围介于0到59之间。Optional seconds, ranging from 0 to 59.
.. 区分区域性的符号, 将秒与秒的小数部分分隔开来。A culture-sensitive symbol that separates seconds from fractions of a second. 固定格式使用句点 (".") 字符。The invariant format uses a period (".") character.
ffff 可选的秒的小数部分, 由1到7个十进制数字组成。Optional fractional seconds, consisting of one to seven decimal digits.

input组件必须共同指定一个大于或TimeSpan.MinValue等于且TimeSpan.MaxValue小于或等于的时间间隔。The components of input must collectively specify a time interval that is greater than or equal to TimeSpan.MinValue and less than or equal to TimeSpan.MaxValue.

TryParse(String, IFormatProvider, TimeSpan)方法尝试使用指定input formatProvider的区域性的每个特定于区域性的格式来进行分析。The TryParse(String, IFormatProvider, TimeSpan) method tries to parse input by using each of the culture-specific formats for the culture specified by formatProvider.

参数是一个IFormatProvider实现, 提供有关返回的字符串格式的区域性特定信息。 formatProviderThe formatProvider parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string. formatProvider参数可以是以下任一项:The formatProvider parameter can be any of the following:

如果formatProvidernull ,DateTimeFormatInfo则使用与当前区域性关联的对象。If formatProvider is null, the DateTimeFormatInfo object that is associated with the current culture is used.

调用方说明Notes to callers

在某些情况下, 当要分析的字符串中的时间间隔组件包含的位数超过7个时, .NET Framework 3.5 及更早版本中成功和返回的分析操作可能会失败并返回到 the.NET 框架中4及更高版本, 下面的示例阐释了这种情况。In some cases, when a time interval component in the string to be parsed contains more than seven digits, parsing operations that succeed and return in the .NET Framework 3.5 and earlier versions may fail and return in the.NET Framework 4 and later versions, The following example illustrates this scenario.

string value = "000000006";
TimeSpan interval;
if (TimeSpan.TryParse(value, out interval))
   Console.WriteLine("{0} --> {1}", value, interval);
else
   Console.WriteLine("Unable to parse '{0}'", value);
// Output from .NET Framework 3.5 and earlier versions:
//       000000006 --> 6.00:00:00
// Output from .NET Framework 4:
//       Unable to parse //000000006//
Dim value As String = "000000006"
Dim interval As TimeSpan
If TimeSpan.TryParse(value, interval) Then
   Console.WriteLine("{0} --> {1}", value, interval)
Else
   Console.WriteLine("Unable to parse '{0}'", value)
End If
' Output from .NET Framework 3.5 and earlier versions:
'       000000006 --> 6.00:00:00
' Output from .NET Framework 4:
'       Unable to parse '000000006'

适用于