TimeSpan.ParseExact TimeSpan.ParseExact TimeSpan.ParseExact TimeSpan.ParseExact Method

定义

将时间间隔的字符串表示形式转换为等效的 TimeSpanConverts the string representation of a time interval to its TimeSpan equivalent. 字符串表示形式的格式必须与指定的格式完全匹配。The format of the string representation must match a specified format exactly.

重载

ParseExact(String, String, IFormatProvider, TimeSpanStyles) ParseExact(String, String, IFormatProvider, TimeSpanStyles) ParseExact(String, String, IFormatProvider, TimeSpanStyles) ParseExact(String, String, IFormatProvider, TimeSpanStyles)

使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 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.

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

使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpanConverts the 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.

ParseExact(String, String[], IFormatProvider) ParseExact(String, String[], IFormatProvider) ParseExact(String, String[], IFormatProvider) ParseExact(String, String[], IFormatProvider)

使用指定的格式字符串数组和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 TimeSpanConverts the string representation of a time interval to its TimeSpan equivalent by using the specified array of format strings and culture-specific format information. 字符串表示形式的格式必须与一种指定的格式完全匹配。The format of the string representation must match one of the specified formats exactly.

ParseExact(String, String, IFormatProvider) ParseExact(String, String, IFormatProvider) ParseExact(String, String, IFormatProvider) ParseExact(String, String, IFormatProvider)

使用指定的格式和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 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.

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles)

ParseExact(String, String, IFormatProvider, TimeSpanStyles) ParseExact(String, String, IFormatProvider, TimeSpanStyles) ParseExact(String, String, IFormatProvider, TimeSpanStyles) ParseExact(String, String, IFormatProvider, TimeSpanStyles)

使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 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 TimeSpan ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles);
static member ParseExact : string * string * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As TimeSpanStyles) As TimeSpan

参数

input
String String String String

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

format
String String String String

用于定义所需的 input 格式的标准或自定义格式字符串。A standard or custom format string that defines the required format of input.

formatProvider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

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

styles
TimeSpanStyles TimeSpanStyles TimeSpanStyles TimeSpanStyles

枚举值的按位组合,用于定义可出现在 input 中的样式元素。A bitwise combination of enumeration values that defines the style elements that may be present in input.

返回

input 对应的时间间隔,由 formatformatProviderstyles 指定。A time interval that corresponds to input, as specified by format, formatProvider, and styles.

异常

styles 不是有效的 TimeSpanStyles 值。styles is an invalid TimeSpanStyles value.

input 的格式无效。input has an invalid format.

input 表示一个小于 MinValue 或大于 MaxValue 的数字。input represents a number that is less than MinValue or greater than MaxValue.

-or- input 中天数、小时、分钟和秒数四个成分中至少一个超出了其有效范围。At least one of the days, hours, minutes, or seconds components in input is outside its valid range.

示例

下面的示例使用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;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }      
      
      // Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48";
      format = "g";
      culture = CultureInfo.InvariantCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153";
      format = @"h\:mm\:ss\.fff";
      culture = null;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", 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;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
            
      // 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;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", 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");
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        null, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        null, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        null, TimeSpanStyles.AssumeNegative);
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
   }
}
// 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
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try      
      
      ' Parse hour:minute:second value with "g" specifier.
      intervalString = "17:14:48"
      format = "g"
      culture = CultureInfo.InvariantCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse hours:minute.second value with custom format specifier.     
      intervalString = "17:14:48.153"
      format = "h\:mm\:ss\.fff"
      culture = Nothing
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' 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
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
            
      ' 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
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' 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")
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        Nothing, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        Nothing, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, 
                                        Nothing, TimeSpanStyles.AssumeNegative)
         Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
   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

注解

方法分析时间间隔的字符串表示形式, 该时间间隔必须采用format参数定义的格式, 但会忽略前导和尾随空白字符。 ParseExactThe ParseExact 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. 由于input必须format严格符合格式, 因此, 在将用户的字符串输入转换为时间间隔时, 应始终使用异常处理。Because input must conform to the format of format exactly, you should always use exception handling when converting a string input by the user to a time interval. 如果不想使用异常处理, 可以改为TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan)调用方法。If you prefer not to use exception handling, you can call the TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) method instead.

参数是一个字符串, 其中包含单个标准格式说明符, 或者一个或多个定义所需的格式的input自定义格式说明符。 formatThe 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是标准 ParseExact 格式字符串(其值为"g"或"g")时,方法才使用formatTimeSpan参数指定的区域性的约定。The ParseExact 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.

参数是一个实现,format提供有关返回字符串格式的区域性特定信息, 前提是标准格式字符串。 IFormatProvider formatProviderThe 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 that are parsed using custom format strings. 它确定是否input仅当存在负号 (TimeSpanStyles.None) 时才将解释为负时间间隔, 或者是否始终解释为负时间间隔 (TimeSpanStyles.AssumeNegative)。It 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.

另请参阅

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

public static TimeSpan ParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
static member ParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, Optional styles As TimeSpanStyles = System.Globalization.TimeSpanStyles.None) As TimeSpan

参数

formats
String[]

返回

ParseExact(String, String[], IFormatProvider, TimeSpanStyles) ParseExact(String, String[], IFormatProvider, TimeSpanStyles) ParseExact(String, String[], IFormatProvider, TimeSpanStyles) ParseExact(String, String[], IFormatProvider, TimeSpanStyles)

使用指定的格式、区域性特定格式信息和样式,将时间间隔的字符串表示形式转换为其等效的 TimeSpanConverts the 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 TimeSpan ParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::TimeSpanStyles styles);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles);
static member ParseExact : string * string[] * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As TimeSpanStyles) As TimeSpan

参数

input
String String String String

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

formats
String[]

用于定义所需的 input 格式的标准或自定义格式字符串的数组。A array of standard or custom format strings that define the required format of input.

formatProvider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

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

styles
TimeSpanStyles TimeSpanStyles TimeSpanStyles TimeSpanStyles

枚举值的按位组合,用于定义可出现在 input 中的样式元素。A bitwise combination of enumeration values that defines the style elements that may be present in input.

返回

input 对应的时间间隔,由 formatsformatProviderstyles 指定。A time interval that corresponds to input, as specified by formats, formatProvider, and styles.

异常

styles 不是有效的 TimeSpanStyles 值。styles is an invalid TimeSpanStyles value.

input 的格式无效。input has an invalid format.

input 表示一个小于 MinValue 或大于 MaxValue 的数字。input represents a number that is less than MinValue or greater than MaxValue.

-or- input 中天数、小时、分钟和秒数四个成分中至少一个超出了其有效范围。At least one of the days, hours, minutes, or seconds components in input is outside its valid range.

示例

下面的示例调用ParseExact(String, String[], IFormatProvider, TimeSpanStyles)方法, 将字符串数组的每个元素转换为一个TimeSpan值。The following example calls the ParseExact(String, String[], IFormatProvider, TimeSpanStyles) 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. 而是使用自定义格式字符串将一个数字解释为小时数。 %hInstead, the %h custom format string is used to interpret a single digit as the number of hours. 要使此更改生效, 请注意%h , 自定义格式字符串必须位于formats数组中的其他格式字符串之前。For 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("de-DE");
      
      // Parse each string in inputs using formats and the de-DE culture.
      foreach (string input in inputs) {
         try {
            interval = TimeSpan.ParseExact(input, formats, culture,
                                           TimeSpanStyles.AssumeNegative);
            Console.WriteLine("{0} --> {1:c}", input, interval);
         }
         catch (FormatException) {
            Console.WriteLine("{0} --> Bad Format", input);
         }      
         catch (OverflowException) {
            Console.WriteLine("{0} --> Overflow", input);   
         }            
      }
   }
}
// The example displays the following output:
//       3 --> -03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       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 de-DE culture.
      For Each input As String In inputs
         Try
            interval = TimeSpan.ParseExact(input, formats, culture, 
                                           TimeSpanStyles.AssumeNegative)
            Console.WriteLine("{0} --> {1:c}", input, interval)   
         Catch e As FormatException
            Console.WriteLine("{0} --> Bad Format", input)   
         Catch e As OverflowException
            Console.WriteLine("{0} --> Overflow", input)   
         End Try            
      Next
   End Sub
End Module
' The example displays the following output:
'       3 --> -03:00:00
'       16:42 --> 16:42:00
'       1:6:52:35.0625 --> Bad Format
'       1:6:52:35,0625 --> 1.06:52:35.0625000

注解

方法分析时间间隔的字符串表示形式, 该时间间隔必须采用formats参数定义的格式之一, 但会忽略前导和尾随空白字符。 ParseExact(String, String[], IFormatProvider, TimeSpanStyles)The ParseExact(String, String[], IFormatProvider, TimeSpanStyles) method parses the string representation of a time interval, which must be in one of the formats defined by the formats parameter, except that leading and trailing white-space characters are ignored. 由于input必须与中formats指定的格式之一完全一致, 因此, 在将用户的字符串输入转换为时间间隔时, 应始终使用异常处理。Because input must exactly conform to one of the formats specified in formats, you should always use exception handling when converting a string input by the user to a time interval. 如果不想使用异常处理, 可以改为TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan)调用方法。If you prefer not to use exception handling, you can call the TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) method instead.

参数是一个字符串数组, 其元素由一个标准格式说明符或一个或多个定义所需格式的input自定义格式说明符组成。 formatsThe 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.AssumeNegative)。It 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.

另请参阅

ParseExact(String, String[], IFormatProvider) ParseExact(String, String[], IFormatProvider) ParseExact(String, String[], IFormatProvider) ParseExact(String, String[], IFormatProvider)

使用指定的格式字符串数组和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 TimeSpanConverts the string representation of a time interval to its TimeSpan equivalent by using the specified array of format strings and culture-specific format information. 字符串表示形式的格式必须与一种指定的格式完全匹配。The format of the string representation must match one of the specified formats exactly.

public:
 static TimeSpan ParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider);
public static TimeSpan ParseExact (string input, string[] formats, IFormatProvider formatProvider);
static member ParseExact : string * string[] * IFormatProvider -> TimeSpan
Public Shared Function ParseExact (input As String, formats As String(), formatProvider As IFormatProvider) As TimeSpan

参数

input
String String String String

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

formats
String[]

用于定义所需的 input 格式的标准或自定义格式字符串的数组。A array of standard or custom format strings that defines the required format of input.

formatProvider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

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

返回

input 对应的时间间隔,由 formatsformatProvider 指定。A time interval that corresponds to input, as specified by formats and formatProvider.

异常

input 的格式无效。input has an invalid format.

input 表示一个小于 MinValue 或大于 MaxValue 的数字。input represents a number that is less than MinValue or greater than MaxValue.

-or- input 中天数、小时、分钟和秒数四个成分中至少一个超出了其有效范围。At least one of the days, hours, minutes, or seconds components in input is outside its valid range.

示例

下面的示例调用ParseExact(String, String[], IFormatProvider)方法, 将字符串数组的每个元素转换为一个TimeSpan值。The following example calls the ParseExact(String, String[], IFormatProvider) method to convert each element of a string array to a TimeSpan value. 该示例使用法语 (法国) 区域性的格式设置约定来解释字符串。The example interprets the strings 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. 而是使用自定义格式字符串将一个数字解释为小时数。 %hInstead, the %h custom format string is used to interpret a single digit as the number of hours. 要使此更改生效, 请注意%h , 自定义格式字符串必须位于formats数组中的其他格式字符串之前。For 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) {
         try {
            interval = TimeSpan.ParseExact(input, formats, culture);
            Console.WriteLine("{0} --> {1:c}", input, interval);
         }
         catch (FormatException) {
            Console.WriteLine("{0} --> Bad Format", input);
         }      
         catch (OverflowException) {
            Console.WriteLine("{0} --> Overflow", input);   
         }            
      }
   }
}
// The example displays the following output:
//       3 --> 03:00:00
//       16:42 --> 16:42:00
//       1:6:52:35.0625 --> Bad Format
//       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
         Try
            interval = TimeSpan.ParseExact(input, formats, culture)
            Console.WriteLine("{0} --> {1:c}", input, interval)   
         Catch e As FormatException
            Console.WriteLine("{0} --> Bad Format", input)   
         Catch e As OverflowException
            Console.WriteLine("{0} --> Overflow", input)   
         End Try            
      Next
   End Sub
End Module
' The example displays the following output:
'       3 --> 3.00:00:00
'       16:42 --> 16:42:00
'       1:6:52:35.0625 --> Bad Format
'       1:6:52:35,0625 --> 1.06:52:35.0625000

注解

方法分析时间间隔的字符串表示形式, 该时间间隔必须采用formats参数定义的格式之一, 但会忽略前导和尾随空白字符。 ParseExact(String, String, IFormatProvider)The ParseExact(String, String, IFormatProvider) method parses the string representation of a time interval, which must be in one of the formats defined by the formats parameter, except that leading and trailing white-space characters are ignored. 由于input必须与中formats指定的格式之一完全一致, 因此, 在将用户的字符串输入转换为时间间隔时, 应始终使用异常处理。Because input must exactly conform to one of the formats specified in formats, you should always use exception handling when converting a string input by the user to a time interval. 如果不想使用异常处理, 可以改为TryParseExact(String, String[], IFormatProvider, TimeSpan)调用方法。If you prefer not to use exception handling, you can call the TryParseExact(String, String[], IFormatProvider, TimeSpan) method instead.

参数是一个字符串数组, 其元素由一个标准格式说明符或一个或多个定义所需格式的input自定义格式说明符组成。 formatsThe 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.

另请参阅

ParseExact(String, String, IFormatProvider) ParseExact(String, String, IFormatProvider) ParseExact(String, String, IFormatProvider) ParseExact(String, String, IFormatProvider)

使用指定的格式和区域性特定格式信息,将时间间隔的字符串表示形式转换为其等效的 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 TimeSpan ParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider);
public static TimeSpan ParseExact (string input, string format, IFormatProvider formatProvider);
static member ParseExact : string * string * IFormatProvider -> TimeSpan
Public Shared Function ParseExact (input As String, format As String, formatProvider As IFormatProvider) As TimeSpan

参数

input
String String String String

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

format
String String String String

用于定义所需的 input 格式的标准或自定义格式字符串。A standard or custom format string that defines the required format of input.

formatProvider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

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

返回

input 对应的时间间隔,由 formatformatProvider 指定。A time interval that corresponds to input, as specified by format and formatProvider.

异常

input 的格式无效。input has an invalid format.

input 表示一个小于 MinValue 或大于 MaxValue 的数字。input represents a number that is less than MinValue or greater than MaxValue.

-or- input 中天数、小时、分钟和秒数四个成分中至少一个超出了其有效范围。At least one of the days, hours, minutes, or seconds components in input is outside its valid range.

示例

下面的示例使用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.

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;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", 
                           intervalString, format);
      }                     
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }      
      
      // Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48";
      format = "G";
      culture = CultureInfo.InvariantCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse hours:minute.second value with "G" specifier 
      // and current (en-US) culture.     
      intervalString = "17:14:48.153";
      format = "G";
      culture = CultureInfo.CurrentCulture;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", 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;
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }   
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", 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");
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse days:hours:minute.second value with "G" specifier 
      // and fr-FR culture.     
      intervalString = "3:17:14:48,153";
      format = "G";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, culture);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }   
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 

      // Parse a single number using the "c" standard format string. 
      intervalString = "12";
      format = "c";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%h" custom format string. 
      format = "%h";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      } 
      
      // Parse a single number using the "%s" custom format string. 
      format = "%s";
      try {
         interval = TimeSpan.ParseExact(intervalString, format, null);
         Console.WriteLine("'{0}' --> {1}", intervalString, interval);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}': Overflow", intervalString);
      }
   }
}
// The example displays the following output:
//       '17:14' --> 17:14:00
//       '17:14:48': Bad Format for 'G'
//       '17:14:48.153': Bad Format for 'G'
//       '3:17:14:48.153' --> 3.17:14:48.1530000
//       '3:17:14:48.153': Bad Format for 'G'
//       '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
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try      
      
      ' Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48"
      format = "G"
      culture = CultureInfo.InvariantCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse hours:minute.second value with "G" specifier 
      ' and current (en-US) culture.     
      intervalString = "17:14:48.153"
      format = "G"
      culture = CultureInfo.CurrentCulture
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' 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
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
            
      ' 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")
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' 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")
      Try
         interval = TimeSpan.ParseExact(intervalString, format, culture)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 

      ' Parse a single number using the "c" standard format string. 
      intervalString = "12"
      format = "c"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, Nothing)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%h" custom format string. 
      format = "%h"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, Nothing)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
      
      ' Parse a single number using the "%s" custom format string. 
      format = "%s"
      Try
         interval = TimeSpan.ParseExact(intervalString, format, Nothing)
         Console.WriteLine("'{0}' --> {1}", intervalString, interval)
      Catch e As FormatException
         Console.WriteLine("'{0}': Bad Format for '{1}'", intervalString, format)
      Catch e As OverflowException
         Console.WriteLine("'{0}': Overflow", intervalString)
      End Try 
   End Sub
End Module
' The example displays the following output:
'       '17:14' --> 17:14:00
'       '17:14:48': Bad Format for 'G'
'       '17:14:48.153': Bad Format for 'G'
'       '3:17:14:48.153' --> 3.17:14:48.1530000
'       '3:17:14:48.153': Bad Format for 'G'
'       '3:17:14:48,153' --> 3.17:14:48.1530000
'       '12' --> 12.00:00:00
'       '12' --> 12:00:00
'       '12' --> 00:00:12

注解

方法分析时间间隔的字符串表示形式, 该时间间隔必须采用format参数定义的格式, 但会忽略前导和尾随空白字符。 ParseExact(String, String, IFormatProvider)The ParseExact(String, String, IFormatProvider) 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. 由于input必须format严格符合格式, 因此, 在将用户的字符串输入转换为时间间隔时, 应始终使用异常处理。Because input must conform to the format of format exactly, you should always use exception handling when converting a string input by the user to a time interval. 如果不想使用异常处理, 可以改为TryParseExact(String, String, IFormatProvider, TimeSpan)调用方法。If you prefer not to use exception handling, you can call the TryParseExact(String, String, IFormatProvider, TimeSpan) method instead.

参数是一个字符串, 其中包含单个标准格式说明符, 或者一个或多个定义所需的格式的input自定义格式说明符。 formatThe 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是标准 ParseExact 格式字符串(其值为"g"或"g")时,方法才使用formatTimeSpan参数指定的区域性的约定。The ParseExact 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.

参数是一个实现,format提供有关返回字符串格式的区域性特定信息, 前提是标准格式字符串。 IFormatProvider formatProviderThe 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.

另请参阅

ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles) ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles)

public static TimeSpan ParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.TimeSpanStyles styles = System.Globalization.TimeSpanStyles.None);
static member ParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.TimeSpanStyles -> TimeSpan
Public Shared Function ParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, Optional styles As TimeSpanStyles = System.Globalization.TimeSpanStyles.None) As TimeSpan

参数

format
ReadOnlySpan<Char>

返回

适用于