TimeSpan.ParseExact Method (String, String(), IFormatProvider, TimeSpanStyles)

 

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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Function ParseExact (
	input As String,
	formats As String(),
	formatProvider As IFormatProvider,
	styles As TimeSpanStyles
) As TimeSpan

Parameters

input
Type: System.String

A string that specifies the time interval to convert.

formats
Type: System.String()

A array of standard or custom format strings that define the required format of input.

formatProvider
Type: System.IFormatProvider

An object that provides culture-specific formatting information.

styles
Type: System.Globalization.TimeSpanStyles

A bitwise combination of enumeration values that defines the style elements that may be present in input.

Return Value

Type: System.TimeSpan

A time interval that corresponds to input, as specified by formats, formatProvider, and styles.

Exception Condition
ArgumentException

styles is an invalid TimeSpanStyles value.

ArgumentNullException

input is null.

FormatException

input has an invalid format.

OverflowException

input represents a number that is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.

-or-

At least one of the days, hours, minutes, or seconds components in input is outside its valid range.

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. 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. If you prefer not to use exception handling, you can call the TryParseExact(String, String(), IFormatProvider, TimeSpanStyles, TimeSpan) method instead.

The 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. For more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings. input must correspond exactly to a member of formats for the parse operation to succeed. The parse operation attempts to match input to each element in formats starting with the first element in the array.

System_CAPS_importantImportant

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

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. The formatProvider parameter can be any of the following:

If formatProvider is null, the DateTimeFormatInfo object that is associated with the current culture is used.

The styles parameter affects the interpretation of strings that are parsed using custom format strings. 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). If TimeSpanStyles.AssumeNegative is not used, format must include a literal negative sign symbol (such as "\-") to successfully parse a negative time interval.

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. Instead, the %h custom format string is used to interpret a single digit as the number of hours. For this change to be effective, note that the %h custom format string must precede the other format strings in the formats array. 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.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1
Return to top
Show: