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.
Assembly: mscorlib (in mscorlib.dll)
public: static bool TryParseExact( String^ input, String^ format, IFormatProvider^ formatProvider, TimeSpanStyles styles, [OutAttribute] TimeSpan% result )
A string that specifies the time interval to convert.
A standard or custom format string that defines the required format of input.
An object that provides culture-specific formatting information.
One or more enumeration values that indicate the style of input.
Return ValueType: System::Boolean
true if input was converted successfully; otherwise, false.
The ParseExact(String^, String^, IFormatProvider^, TimeSpanStyles) method, except that it does not throw an exception if the conversion fails.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
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:
A CultureInfo object that represents the culture whose formatting conventions are to be reflected in the returned string. The DateTimeFormatInfo object returned by the CultureInfo::DateTimeFormat property defines the formatting of the returned string.
A DateTimeFormatInfo object that defines the formatting of the returned string.
If formatProvider is null, 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.
Available since 8
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Available since 8.1