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

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

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

'Declaration
Public Shared Function TryParseExact ( _
	input As String, _
	format As String, _
	formatProvider As IFormatProvider, _
	styles As TimeSpanStyles, _
	<OutAttribute> ByRef result As TimeSpan _
) As Boolean

Parameters

input
Type: System.String

A string that specifies the time interval to convert.

format
Type: System.String

A standard or custom format string that defines the required format of input.

formatProvider
Type: System.IFormatProvider

An object that provides culture-specific formatting information.

styles
Type: System.Globalization.TimeSpanStyles

One or more enumeration values that indicate the style of input.

result
Type: System.TimeSpan

When this method returns, contains an object that represents the time interval specified by input, or TimeSpan.Zero if the conversion failed. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if input was converted successfully; otherwise, false.

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. This method is similar to the ParseExact(String, String, IFormatProvider, TimeSpanStyles) method, except that it does not throw an exception if the conversion fails.

The 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. For more information about valid format strings, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.

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

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

The styles parameter affects the interpretation of strings 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 uses the ParseExact(String, String, IFormatProvider) method to parse several string representations of time intervals using various format strings and cultures. It also uses the TimeSpanStyles.AssumeNegative value to interpret each string as a negative time interval. The output from the example illustrates that the TimeSpanStyles.AssumeNegative style affects the return value only when it is used with custom format strings.

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

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft