Export (0) Print
Expand All

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

Converts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information, and returns a value that indicates whether the conversion succeeded. The format of the string representation must match one of the specified formats exactly.

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

public static bool TryParseExact(
	string input,
	string[] formats,
	IFormatProvider formatProvider,
	out TimeSpan result
)

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 acceptable formats of input.

formatProvider
Type: System.IFormatProvider

An object that provides culture-specific formatting information.

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

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.

Important noteImportant

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". 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 following example calls the TryParseExact(String, String[], IFormatProvider, TimeSpan) method to convert each element of a string array to a TimeSpan value. 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. 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.

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

.NET Framework

Supported in: 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft