TimeSpan.TryParseExact 方法

定義

將時間間隔的字串表示,轉換為其相等的 TimeSpan,並傳回一個值表示轉換是否成功。Converts the string representation of a time interval to its TimeSpan equivalent, and returns a value that indicates whether the conversion succeeded. 字串表示的格式必須完全符合指定的格式。The format of the string representation must match a specified format exactly.

多載

TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan)

使用指定的格式、文化特性專屬格式資訊及樣式,將時間間隔的指定字串表示法轉換成對等的 TimeSpanConverts the string representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information and styles. 字串表示的格式必須完全符合指定的格式。The format of the string representation must match the specified format exactly.

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles, TimeSpan)

使用指定的格式、文化特性專屬格式資訊及樣式,將時間間隔的指定範圍表示法轉換成對等的 TimeSpanConverts the specified span representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information and styles. 字串表示的格式必須完全符合其中一個指定的格式。The format of the string representation must match one of the specified formats exactly.

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles, TimeSpan)

使用指定格式、特定文件特性格式資訊及樣式,將指定的時間間隔範圍表示轉換為其對等的 TimeSpan,並傳回值以指出轉換是否成功。Converts the specified span representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information, and styles, and returns a value that indicates whether the conversion succeeded. 字串表示的格式必須完全符合指定的格式。The format of the string representation must match the specified format exactly.

TryParseExact(String, String[], IFormatProvider, TimeSpan)

使用指定的格式和文化特性專屬格式資訊,將時間間隔的指定字串表示法轉換成對等的 TimeSpanConverts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information. 字串表示的格式必須完全符合其中一個指定的格式。The format of the string representation must match one of the specified formats exactly.

TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan)

使用指定的格式、文化特性專屬格式資訊及樣式,將時間間隔的指定字串表示法轉換成對等的 TimeSpanConverts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information and styles. 字串表示的格式必須完全符合其中一個指定的格式。The format of the string representation must match one of the specified formats exactly.

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpan)

使用指定的格式和文化特性專屬格式資訊,將時間間隔的指定範圍表示法轉換成對等的 TimeSpanConverts the specified span representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information. 字串表示的格式必須完全符合其中一個指定的格式。The format of the string representation must match one of the specified formats exactly.

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

使用指定的格式和文化特性專屬格式資訊,將時間間隔的指定範圍表示法轉換成對等的 TimeSpanConverts the specified span representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. 字串表示的格式必須完全符合指定的格式。The format of the string representation must match the specified format exactly.

TryParseExact(String, String, IFormatProvider, TimeSpan)

使用指定之格式和特定文化特性格式資訊,將時間間隔的字串表示轉換為其相等的 TimeSpanConverts the string representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. 字串表示的格式必須完全符合指定的格式。The format of the string representation must match the specified format exactly.

TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan)

使用指定的格式、文化特性專屬格式資訊及樣式,將時間間隔的指定字串表示法轉換成對等的 TimeSpanConverts the string representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information and styles. 字串表示的格式必須完全符合指定的格式。The format of the string representation must match the specified format exactly.

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

參數

input
String

字串,指定要轉換的時間間隔。A string that specifies the time interval to convert.

format
String

標準或自訂格式字串,其定義 input 的必要格式。A standard or custom format string that defines the required format of input.

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that provides culture-specific formatting information.

styles
TimeSpanStyles

一或多個列舉值,這些值會指出 input 的樣式。One or more enumeration values that indicate the style of input.

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

傳回

Boolean

如果 input 轉換成功,則為 true,否則為 falsetrue if input was converted successfully; otherwise, false.

範例

下列範例會使用 ParseExact(String, String, IFormatProvider) 方法,利用各種格式字串和文化特性來剖析時間間隔的數個字串表示。The following example uses the ParseExact(String, String, IFormatProvider) method to parse several string representations of time intervals using various format strings and cultures. 它也會使用 TimeSpanStyles.AssumeNegative 值,將每個字串解讀為負的時間間隔。It also uses the TimeSpanStyles.AssumeNegative value to interpret each string as a negative time interval. 範例的輸出說明, TimeSpanStyles.AssumeNegative 只有在搭配自訂格式字串使用時,樣式才會影響傳回值。The output from the example illustrates that the TimeSpanStyles.AssumeNegative style affects the return value only when it is used with custom format strings.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString, format;
      TimeSpan interval;
      CultureInfo culture = null;
      
      // Parse hour:minute value with custom format specifier.
      intervalString = "17:14";
      format = "h\\:mm";
      culture = CultureInfo.CurrentCulture;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48";
      format = "g";
      culture = CultureInfo.InvariantCulture;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153";
      format = @"h\:mm\:ss\.fff";
      culture = null;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);   

      // Parse days:hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);   
            
      // Parse days:hours:minute.second value with a custom format specifier.     
      intervalString = "3:17:14:48.153";
      format = @"d\:hh\:mm\:ss\.fff";
      culture = null;
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48,153";
      format = "G";
      culture = new CultureInfo("fr-FR");
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 culture, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 null, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 null, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      if (TimeSpan.TryParseExact(intervalString, format, 
                                 null, TimeSpanStyles.AssumeNegative, out interval))
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      else   
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format);
   }
}
// The example displays the following output:
//    '17:14' (h\:mm) --> -17:14:00
//    '17:14:48' (g) --> 17:14:48
//    '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
//    '3:17:14:48.153' (G) --> 3.17:14:48.1530000
//    '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
//    '3:17:14:48,153' (G) --> 3.17:14:48.1530000
//    '12' (c) --> 12.00:00:00
//    '12' (%h) --> -12:00:00
//    '12' (%s) --> -00:00:12
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim intervalString, format As String
      Dim interval As TimeSpan
      Dim culture As CultureInfo = Nothing
      
      ' Parse hour:minute value with custom format specifier.
      intervalString = "17:14"
      format = "h\:mm"
      culture = CultureInfo.CurrentCulture
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If
      
      ' Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48"
      format = "g"
      culture = CultureInfo.InvariantCulture
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If
      
      ' Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153"
      format = "h\:mm\:ss\.fff"
      culture = Nothing
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 

      ' Parse days:hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "3:17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
            
      ' Parse days:hours:minute.second value with a custom format specifier.     
      intervalString = "3:17:14:48.153"
      format = "d\:hh\:mm\:ss\.fff"
      culture = Nothing
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
      
      ' Parse days:hours:minute.second value with "G" specifier 
      ' and fr-FR culture.     
      intervalString = "3:17:14:48,153"
      format = "G"
      culture = New CultureInfo("fr-FR")
      If TimeSpan.TryParseExact(intervalString, format, 
                                culture, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)
      End If 

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      If TimeSpan.TryParseExact(intervalString, format, 
                                Nothing, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      If TimeSpan.TryParseExact(intervalString, format, 
                                Nothing, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      If TimeSpan.TryParseExact(intervalString, format, 
                                Nothing, TimeSpanStyles.AssumeNegative, interval) Then
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Else
         Console.WriteLine("Unable to parse '{0}' using format {1}",
                           intervalString, format)   
      End If 
   End Sub
End Module
' The example displays the following output:
'    '17:14' (h\:mm) --> -17:14:00
'    '17:14:48' (g) --> 17:14:48
'    '17:14:48.153' (h\:mm\:ss\.fff) --> -17:14:48.1530000
'    '3:17:14:48.153' (G) --> 3.17:14:48.1530000
'    '3:17:14:48.153' (d\:hh\:mm\:ss\.fff) --> -3.17:14:48.1530000
'    '3:17:14:48,153' (G) --> 3.17:14:48.1530000
'    '12' (c) --> 12.00:00:00
'    '12' (%h) --> -12:00:00
'    '12' (%s) --> -00:00:12

備註

TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan)方法會剖析時間間隔的字串表示,其必須是參數所定義的格式 format ,但會忽略開頭和尾端空白字元。The TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) method parses the string representation of a time interval, which must be in the format defined by the format parameter, except that leading and trailing white-space characters are ignored. 此方法與 ParseExact(String, String, IFormatProvider, TimeSpanStyles) 方法類似,不同之處在于如果轉換失敗,則不會擲回例外狀況。This method is similar to the ParseExact(String, String, IFormatProvider, TimeSpanStyles) method, except that it does not throw an exception if the conversion fails.

format參數是包含單一標準格式規範的字串,或是定義所需格式的一或多個自訂格式規範 inputThe format parameter is a string that contains either a single standard format specifier, or one or more custom format specifiers that define the required format of input. 如需有效格式字串的詳細資訊,請參閱 標準 Timespan 格式字串自訂 timespan 格式字串For more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.

formatProvider如果是標準格式字串,則參數是一個 IFormatProvider 執行,可提供有關所傳回字串格式的文化特性特定資訊 formatThe formatProvider parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string if format is a standard format 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.

styles 參數會影響使用自訂格式字串剖析之字串的解讀。The styles parameter affects the interpretation of strings parsed using custom format strings. 它會判斷是否 input 只有當負號 (TimeSpanStyles.None) ,或是否一律將它轉譯為 () 的負時間間隔時,才會被視為負的時間間隔 TimeSpanStyles.AssumeNegativeIt determines whether input is interpreted as a negative time interval only if a negative sign is present (TimeSpanStyles.None), or whether it is always interpreted as a negative time interval (TimeSpanStyles.AssumeNegative). 如果 TimeSpanStyles.AssumeNegative 未使用,則 format 必須包含常值負號符號 (例如 " \ -" ) ,才能成功剖析負的時間間隔。If TimeSpanStyles.AssumeNegative is not used, format must include a literal negative sign symbol (such as "\-") to successfully parse a negative time interval.

適用於

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles, TimeSpan)

使用指定的格式、文化特性專屬格式資訊及樣式,將時間間隔的指定範圍表示法轉換成對等的 TimeSpanConverts the specified span representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information and styles. 字串表示的格式必須完全符合其中一個指定的格式。The format of the string representation must match one of the specified formats exactly.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan? result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
static member TryParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.TimeSpanStyles * TimeSpan -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, styles As TimeSpanStyles, ByRef result As TimeSpan) As Boolean

參數

input
ReadOnlySpan<Char>

範圍,其包含代表要轉換時間間隔的字元。A span containing the characters that represent a time interval to convert.

formats
String[]

標準或自訂格式字串的陣列,這些格式字串會定義 input 可接受的格式。An array of standard or custom format strings that define the acceptable formats of input.

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

styles
TimeSpanStyles

一或多個列舉值,這些值會指出 input 的樣式。One or more enumeration values that indicate the style of input.

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

傳回

Boolean

如果 input 轉換成功,則為 true,否則為 falsetrue if input was converted successfully; otherwise, false.

適用於

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles, TimeSpan)

使用指定格式、特定文件特性格式資訊及樣式,將指定的時間間隔範圍表示轉換為其對等的 TimeSpan,並傳回值以指出轉換是否成功。Converts the specified span representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information, and styles, and returns a value that indicates whether the conversion succeeded. 字串表示的格式必須完全符合指定的格式。The format of the string representation must match the specified format exactly.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.TimeSpanStyles * TimeSpan -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, styles As TimeSpanStyles, ByRef result As TimeSpan) As Boolean

參數

input
ReadOnlySpan<Char>

範圍,其包含代表要轉換時間間隔的字元。A span containing the characters that represent a time interval to convert.

format
ReadOnlySpan<Char>

包含字元的範圍,這些字元代表的標準或自訂格式字串會定義 input 可接受的格式。A span containing the charactes that represent a standard or custom format string that defines the acceptable format of input.

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

styles
TimeSpanStyles

一或多個列舉值,這些值會指出 input 的樣式。One or more enumeration values that indicate the style of input.

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

傳回

Boolean

如果 input 轉換成功,則為 true,否則為 falsetrue if input was converted successfully; otherwise, false.

適用於

TryParseExact(String, String[], IFormatProvider, TimeSpan)

使用指定的格式和文化特性專屬格式資訊,將時間間隔的指定字串表示法轉換成對等的 TimeSpanConverts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information. 字串表示的格式必須完全符合其中一個指定的格式。The format of the string representation must match one of the specified formats exactly.

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

參數

input
String

字串,指定要轉換的時間間隔。A string that specifies the time interval to convert.

formats
String[]

標準或自訂格式字串的陣列,這些格式字串會定義 input 可接受的格式。An array of standard or custom format strings that define the acceptable formats of input.

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that provides culture-specific formatting information.

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

傳回

Boolean

如果 input 轉換成功,則為 true,否則為 falsetrue if input was converted successfully; otherwise, false.

範例

下列範例會呼叫 TryParseExact(String, String[], IFormatProvider, TimeSpan) 方法,將字串陣列的每個元素轉換成 TimeSpan 值。The following example calls the TryParseExact(String, String[], IFormatProvider, TimeSpan) method to convert each element of a string array to a TimeSpan value. 此範例會使用法文-法國 ( "fr-fr" ) 文化特性的格式化慣例來解讀字串。The example interprets the strings by using the formatting conventions of the French - France ("fr-FR") culture. 字串可以代表一般簡短格式或一般完整格式的時間間隔。The strings can represent a time interval in either the general short format or the general long format.

此外,此範例會變更時間間隔剖析方法解讀單一數位的方式。In addition, the example changes the way in which the time interval parsing methods interpret a single digit. 一般來說,單一數位會被視為時間間隔中的天數。Ordinarily, a single digit is interpreted as the number of days in a time interval. 相反地, %h 自訂格式字串會用來將單一位數解讀為時數。Instead, the %h custom format string is used to interpret a single digit as the number of hours. 若要讓這項變更生效,請注意 %h 自訂格式字串必須在陣列中的其他格式字串之前 formatsFor this change to be effective, note that the %h custom format string must precede the other format strings in the formats array.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] inputs = { "3", "16:42", "1:6:52:35.0625", 
                          "1:6:52:35,0625" }; 
      string[] formats = { "g", "G", "%h"};
      TimeSpan interval;
      CultureInfo culture = new CultureInfo("fr-FR");
      
      // Parse each string in inputs using formats and the fr-FR culture.
      foreach (string input in inputs) {
         if(TimeSpan.TryParseExact(input, formats, culture, out interval))
            Console.WriteLine("{0} --> {1:c}", input, interval);
         else
            Console.WriteLine("Unable to parse {0}", input);   
      }
   }
}
// The example displays the following output:
//       3 --> 03:00:00
//       16:42 --> 16:42:00
//       Unable to parse 1:6:52:35.0625
//       1:6:52:35,0625 --> 1.06:52:35.0625000
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim inputs() As String = { "3", "16:42", "1:6:52:35.0625", 
                                 "1:6:52:35,0625" } 
      Dim formats() As String = { "%h", "g", "G" }
      Dim interval As TimeSpan
      Dim culture As New CultureInfo("fr-FR")
      
      ' Parse each string in inputs using formats and the fr-FR culture.
      For Each input As String In inputs
         If TimeSpan.TryParseExact(input, formats, culture, interval) Then
            Console.WriteLine("{0} --> {1:c}", input, interval)   
         Else
            Console.WriteLine("Unable to parse {0}", input)   
         End If            
      Next
   End Sub
End Module
' The example displays the following output:
'       3 --> 03:00:00
'       16:42 --> 16:42:00
'       Unable to parse 1:6:52:35.0625
'       1:6:52:35,0625 --> 1.06:52:35.0625000

備註

TryParseExact(String, String[], IFormatProvider, TimeSpan)方法會剖析時間間隔的字串表示,這必須是參數所指定的其中一個格式字串所定義的格式 formats ,但會忽略開頭和尾端空白字元。The TryParseExact(String, String[], IFormatProvider, TimeSpan) method parses the string representation of a time interval, which must be in the format defined by one of the format strings specified by the formats parameter, except that leading and trailing white-space characters are ignored. 此方法與 ParseExact(String, String[], IFormatProvider) 方法類似,不同之處在于如果轉換失敗,則不會擲回例外狀況。This method is similar to the ParseExact(String, String[], IFormatProvider) method, except that it does not throw an exception if the conversion fails.

formats參數是字串陣列,其元素是由單一標準格式規範所組成,或是定義所需格式的一或多個自訂格式規範 inputThe formats parameter is a string array whose elements consist of either a single standard format specifier, or one or more custom format specifiers that define the required format of input. 如需有效格式字串的詳細資訊,請參閱 標準 Timespan 格式字串自訂 timespan 格式字串For more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings. input 必須完全對應至的成員, formats 剖析作業才會成功。input must correspond exactly to a member of formats for the parse operation to succeed. 剖析作業會嘗試比對 input 開頭為數組中 formats 第一個元素的每個專案。The parse operation attempts to match input to each element in formats starting with the first element in the array.

重要

TryParseExact formatProvider 只有當用來剖析的格式字串 input 是標準 TimeSpan 格式字串(其值為 "G" 或 "g")時,此方法才會使用參數所指定之文化特性的慣例。The TryParseExact method uses the conventions of the culture specified by the formatProvider parameter only if the format string used to parse input is a standard TimeSpan format string whose value is either "g" or "G". "C"、"t" 和 "T" 標準格式字串會使用不變文化特性的格式化慣例。The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. 自訂格式字串會定義輸入字串的精確格式,並使用常值字元來分隔時間間隔的元件。Custom format strings define the precise format of the input string and use literal characters to separate the components of a time interval.

formatProvider IFormatProvider 如果用來剖析的格式字串 input 是標準格式字串,則參數是可提供所傳回字串格式的特定文化特性資訊的實作為。The formatProvider parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string if the format string used to parse input is a standard format 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.

適用於

TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan)

使用指定的格式、文化特性專屬格式資訊及樣式,將時間間隔的指定字串表示法轉換成對等的 TimeSpanConverts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information and styles. 字串表示的格式必須完全符合其中一個指定的格式。The format of the string representation must match one of the specified formats exactly.

public:
 static bool TryParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan result);
public static bool TryParseExact (string? input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan? result);
public static bool TryParseExact (string? input, string[] formats, IFormatProvider? formatProvider, System.Globalization.TimeSpanStyles styles, out TimeSpan? result);
static member TryParseExact : string * string[] * IFormatProvider * System.Globalization.TimeSpanStyles * TimeSpan -> bool
Public Shared Function TryParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As TimeSpanStyles, ByRef result As TimeSpan) As Boolean

參數

input
String

字串,指定要轉換的時間間隔。A string that specifies the time interval to convert.

formats
String[]

標準或自訂格式字串的陣列,這些格式字串會定義 input 可接受的格式。An array of standard or custom format strings that define the acceptable formats of input.

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

styles
TimeSpanStyles

一或多個列舉值,這些值會指出 input 的樣式。One or more enumeration values that indicate the style of input.

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

傳回

Boolean

如果 input 轉換成功,則為 true,否則為 falsetrue if input was converted successfully; otherwise, false.

範例

下列範例會呼叫 TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) 方法,將字串陣列的每個元素轉換成 TimeSpan 值。The following example calls the TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) method to convert each element of a string array to a TimeSpan value. 字串可以代表一般簡短格式或一般完整格式的時間間隔。The strings can represent a time interval in either the general short format or the general long format.

此外,此範例會變更時間間隔剖析方法解讀單一數位的方式。In addition, the example changes the way in which the time interval parsing methods interpret a single digit. 一般來說,單一數位會被視為時間間隔中的天數。Ordinarily, a single digit is interpreted as the number of days in a time interval. 相反地, %h 自訂格式字串會用來將單一位數解讀為時數。Instead, the %h custom format string is used to interpret a single digit as the number of hours. 若要讓這項變更生效,請注意 %h 自訂格式字串必須在陣列中的其他格式字串之前 formatsFor this change to be effective, note that the %h custom format string must precede the other format strings in the formats array. 另請注意, TimeSpanStyles.AssumeNegative 只有在使用此格式規範剖析字串時,才會使用方法呼叫中所指定的旗標。Also note from the output that the TimeSpanStyles.AssumeNegative flag specified in the method call is used only when parsing a string with this format specifier.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] inputs = { "3", "16:42", "1:6:52:35.0625", 
                          "1:6:52:35,0625" }; 
      string[] formats = { "%h", "g", "G" };
      TimeSpan interval;
      CultureInfo culture = new CultureInfo("fr-FR");
      
      // Parse each string in inputs using formats and the fr-FR culture.
      foreach (string input in inputs) {
         if(TimeSpan.TryParseExact(input, formats, culture, 
                                   TimeSpanStyles.AssumeNegative, out interval))
            Console.WriteLine("{0} --> {1:c}", input, interval);
         else
            Console.WriteLine("Unable to parse {0}", input);   
      }
   }
}
// The example displays the following output:
//       3 --> -03:00:00
//       16:42 --> 16:42:00
//       Unable to parse 1:6:52:35.0625
//       1:6:52:35,0625 --> 1.06:52:35.0625000
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim inputs() As String = { "3", "16:42", "1:6:52:35.0625", 
                                 "1:6:52:35,0625" } 
      Dim formats() As String = { "%h", "g", "G" }
      Dim interval As TimeSpan
      Dim culture As New CultureInfo("de-DE")
      
      ' Parse each string in inputs using formats and the fr-FR culture.
      For Each input As String In inputs
         If TimeSpan.TryParseExact(input, formats, culture, 
                                   TimeSpanStyles.AssumeNegative, interval) Then
            Console.WriteLine("{0} --> {1:c}", input, interval)   
         Else
            Console.WriteLine("Unable to parse {0}", input)   
         End If            
      Next
   End Sub
End Module
' The example displays the following output:
'       3 --> -03:00:00
'       16:42 --> 16:42:00
'       Unable to parse 1:6:52:35.0625
'       1:6:52:35,0625 --> 1.06:52:35.0625000

備註

TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan)方法會剖析時間間隔的字串表示,這必須是參數所指定的其中一個格式字串所定義的格式 formats ,但會忽略開頭和尾端空白字元。The TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) method parses the string representation of a time interval, which must be in the format defined by one of the format strings specified by the formats parameter, except that leading and trailing white-space characters are ignored. 此方法與 ParseExact(String, String[], IFormatProvider, TimeSpanStyles) 方法類似,不同之處在于如果轉換失敗,則不會擲回例外狀況。This method is similar to the ParseExact(String, String[], IFormatProvider, TimeSpanStyles) method, except that it does not throw an exception if the conversion fails.

formats參數是字串陣列,其元素是由單一標準格式規範所組成,或是定義所需格式的一或多個自訂格式規範 inputThe formats parameter is a string array whose elements consist of either a single standard format specifier, or one or more custom format specifiers that define the required format of input. 如需有效格式字串的詳細資訊,請參閱 標準 Timespan 格式字串自訂 timespan 格式字串For more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings. input 必須完全對應至的成員, formats 剖析作業才會成功。input must correspond exactly to a member of formats for the parse operation to succeed. 剖析作業會嘗試比對 input 開頭為數組中 formats 第一個元素的每個專案。The parse operation attempts to match input to each element in formats starting with the first element in the array.

重要

ParseExact formatProvider 只有當用來剖析的格式字串 input 是標準 TimeSpan 格式字串(其值為 "G" 或 "g")時,此方法才會使用參數所指定之文化特性的慣例。The ParseExact method uses the conventions of the culture specified by the formatProvider parameter only if the format string used to parse input is a standard TimeSpan format string whose value is either "g" or "G". "C"、"t" 和 "T" 標準格式字串會使用不變文化特性的格式化慣例。The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. 自訂格式字串會定義輸入字串的精確格式,並使用常值字元來分隔時間間隔的元件。Custom format strings define the precise format of the input string and use literal characters to separate the components of a time interval.

formatProvider IFormatProvider 如果用來剖析的格式字串 input 是標準格式字串,則參數是可提供所傳回字串格式的特定文化特性資訊的實作為。The formatProvider parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string if the format string used to parse input is a standard format 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.

styles 參數會影響使用自訂格式字串剖析之字串的解讀。The styles parameter affects the interpretation of strings that are parsed using custom format strings. 它會判斷是否 input 只有當負號 (TimeSpanStyles.None) ,或是否一律將它轉譯為 () 的負時間間隔時,才會被視為負的時間間隔 TimeSpanStyles.AssumeNegativeIt determines whether input is interpreted as a negative time interval only if a negative sign is present (TimeSpanStyles.None), or whether it is always interpreted as a negative time interval (TimeSpanStyles.AssumeNegative). 如果 TimeSpanStyles.AssumeNegative 未使用,則 format 必須包含常值負號符號 (例如 " \ -" ) ,才能成功剖析負的時間間隔。If TimeSpanStyles.AssumeNegative is not used, format must include a literal negative sign symbol (such as "\-") to successfully parse a negative time interval.

適用於

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpan)

使用指定的格式和文化特性專屬格式資訊,將時間間隔的指定範圍表示法轉換成對等的 TimeSpanConverts the specified span representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information. 字串表示的格式必須完全符合其中一個指定的格式。The format of the string representation must match one of the specified formats exactly.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? formatProvider, out TimeSpan? result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider? formatProvider, out TimeSpan result);
static member TryParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * TimeSpan -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean

參數

input
ReadOnlySpan<Char>

範圍,其包含代表要轉換時間間隔的字元。A span containing the characters that represent a time interval to convert.

formats
String[]

標準或自訂格式字串的陣列,這些格式字串會定義 input 可接受的格式。An array of standard or custom format strings that define the acceptable formats of input.

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

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

傳回

Boolean

如果 input 轉換成功,則為 true,否則為 falsetrue if input was converted successfully; otherwise, false.

適用於

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

使用指定的格式和文化特性專屬格式資訊,將時間間隔的指定範圍表示法轉換成對等的 TimeSpanConverts the specified span representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. 字串表示的格式必須完全符合指定的格式。The format of the string representation must match the specified format exactly.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider ^ formatProvider, [Runtime::InteropServices::Out] TimeSpan % result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, out TimeSpan result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, out TimeSpan result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * TimeSpan -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, ByRef result As TimeSpan) As Boolean

參數

input
ReadOnlySpan<Char>

範圍,其包含代表要轉換時間間隔的字元。A span containing the characters that represent a time interval to convert.

format
ReadOnlySpan<Char>

包含字元的範圍,這些字元代表的標準或自訂格式字串能定義 input 可接受的格式。A span containing the characters that represent a standard or custom format string that defines the acceptable format of input.

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

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

傳回

Boolean

如果 input 轉換成功,則為 true,否則為 falsetrue if input was converted successfully; otherwise, false.

適用於

TryParseExact(String, String, IFormatProvider, TimeSpan)

使用指定之格式和特定文化特性格式資訊,將時間間隔的字串表示轉換為其相等的 TimeSpanConverts the string representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. 字串表示的格式必須完全符合指定的格式。The format of the string representation must match the specified format exactly.

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

參數

input
String

字串,指定要轉換的時間間隔。A string that specifies the time interval to convert.

format
String

標準或自訂格式字串,其定義 input 的必要格式。A standard or custom format string that defines the required format of input.

formatProvider
IFormatProvider

物件,提供特定文化特性格式資訊。An object that supplies culture-specific formatting information.

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

傳回

Boolean

如果 input 轉換成功,則為 true,否則為 falsetrue if input was converted successfully; otherwise, false.

範例

下列範例會使用 TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) 方法,利用各種格式字串和文化特性來剖析時間間隔的數個字串表示。The following example uses the TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) method to parse several string representations of time intervals using various format strings and cultures.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString, format;
      TimeSpan interval;
      CultureInfo culture;
      
      // Parse hour:minute value with "g" specifier current culture.
      intervalString = "17:14";
      format = "g";
      culture = CultureInfo.CurrentCulture;
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48";
      format = "G";
      culture = CultureInfo.InvariantCulture;
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);

      // Parse days:hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
            
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48.153";
      format = "G";
      culture = new CultureInfo("fr-FR");
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48,153";
      format = "G";
      if (TimeSpan.TryParseExact(intervalString, format, culture, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      if (TimeSpan.TryParseExact(intervalString, format, null, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      if (TimeSpan.TryParseExact(intervalString, format, null, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      if (TimeSpan.TryParseExact(intervalString, format, null, out interval))
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      else
         Console.WriteLine("Unable to parse {0}", intervalString);
   }
}
// The example displays the following output:
//       '17:14' --> 17:14:00
//       Unable to parse 17:14:48
//       Unable to parse 17:14:48.153
//       '3:17:14:48.153' --> 3.17:14:48.1530000
//       Unable to parse 3:17:14:48.153
//       '3:17:14:48,153' --> 3.17:14:48.1530000
//       '12' --> 12.00:00:00
//       '12' --> 12:00:00
//       '12' --> 00:00:12
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim intervalString, format As String
      Dim interval As TimeSpan
      Dim culture As CultureInfo
      
      ' Parse hour:minute value with "g" specifier current culture.
      intervalString = "17:14"
      format = "g"
      culture = CultureInfo.CurrentCulture
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
      
      ' Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48"
      format = "G"
      culture = CultureInfo.InvariantCulture
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If

      ' Parse hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If

      ' Parse days:hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "3:17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
            
      ' Parse days:hours:minute.second value with "G" specifier 
      ' and fr-FR culture.     
      intervalString = "3:17:14:48.153"
      format = "G"
      culture = New CultureInfo("fr-FR")
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
      
      ' Parse days:hours:minute.second value with "G" specifier 
      ' and fr-FR culture.     
      intervalString = "3:17:14:48,153"
      format = "G"
      culture = New CultureInfo("fr-FR")
      If TimeSpan.TryParseExact(intervalString, format, culture, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      If TimeSpan.TryParseExact(intervalString, format, Nothing, interval)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      If TimeSpan.TryParseExact(intervalString, format, Nothing, interval)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      If TimeSpan.TryParseExact(intervalString, format, Nothing, interval) Then
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Else
         Console.WriteLine("Unable to parse {0}", intervalString)
      End If
   End Sub
End Module
' The example displays the following output:
'       '17:14' --> 17:14:00
'       Unable to parse 17:14:48
'       Unable to parse 17:14:48.153
'       '3:17:14:48.153' --> 3.17:14:48.1530000
'       Unable to parse 3:17:14:48.153
'       '3:17:14:48,153' --> 3.17:14:48.1530000
'       '12' --> 12.00:00:00
'       '12' --> 12:00:00
'       '12' --> 00:00:12

備註

TryParseExact(String, String, IFormatProvider, TimeSpan)方法會剖析時間間隔的字串表示,其必須是參數所定義的格式 format ,但會忽略開頭和尾端空白字元。The TryParseExact(String, String, IFormatProvider, TimeSpan) method parses the string representation of a time interval, which must be in the format defined by the format parameter, except that leading and trailing white-space characters are ignored. 此方法與 ParseExact(String, String, IFormatProvider) 方法類似,不同之處在于如果轉換失敗,則不會擲回例外狀況。This method is similar to the ParseExact(String, String, IFormatProvider) method, except that it does not throw an exception if the conversion fails.

format參數是包含單一標準格式規範的字串,或是定義所需格式的一或多個自訂格式規範 inputThe format parameter is a string that contains either a single standard format specifier, or one or more custom format specifiers that define the required format of input. 如需有效格式字串的詳細資訊,請參閱 標準 Timespan 格式字串自訂 timespan 格式字串For more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.

重要

TryParseExact(String, String, IFormatProvider, TimeSpan) formatProvider 只有當 format 是標準 TimeSpan 格式字串(其值為 "G" 或 "g")時,此方法才會使用參數所指定的文化特性慣例。The TryParseExact(String, String, IFormatProvider, TimeSpan) method uses the conventions of the culture specified by the formatProvider parameter only if format is a standard TimeSpan format string whose value is either "g" or "G". "C"、"t" 和 "T" 標準格式字串會使用不變文化特性的格式化慣例。The "c", "t", and "T" standard format strings use the formatting conventions of the invariant culture. 自訂格式字串會定義輸入字串的精確格式,並使用常值字元來分隔時間間隔的元件。Custom format strings define the precise format of the input string and use literal characters to separate the components of a time interval.

formatProvider如果是標準格式字串,則參數是一個 IFormatProvider 執行,可提供有關所傳回字串格式的文化特性特定資訊 formatThe formatProvider parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string if format is a standard format 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.

適用於