TimeSpan::TryParseExact Method (String^, String^, IFormatProvider^, TimeSpan%)


Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information, 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)

static bool TryParseExact(
	String^ input,
	String^ format,
	IFormatProvider^ formatProvider,
	[OutAttribute] TimeSpan% result


Type: System::String^

A string that specifies the time interval to convert.

Type: System::String^

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

Type: System::IFormatProvider^

An object that supplies culture-specific formatting information.

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 the format 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 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 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". 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 format 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 uses the TryParseExact(String^, String^, IFormatProvider^, TimeSpanStyles, TimeSpan%) method to parse several string representations of time intervals using various format strings and cultures.

No code example is currently available or this language may not be supported.

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