TimeSpan.ParseExact Method (String, String, IFormatProvider)
Converts the string representation of a time interval to its TimeSpan equivalent by using the specified array of format strings and culture-specific format information. The format of the string representation must match one of the specified formats exactly.
Assembly: mscorlib (in mscorlib.dll)
static member ParseExact : input:string * formats:string * formatProvider:IFormatProvider -> TimeSpan
A string that specifies the time interval to convert.
A array of standard or custom format strings that defines the required format of input.
An object that provides culture-specific formatting information.
Return ValueType: System.TimeSpan
A time interval that corresponds to input, as specified by formats and formatProvider.
input is null.
input has an invalid format.
The ParseExact(String, String, IFormatProvider) 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, 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.
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:
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 following example calls the TimeSpan value. The example interprets the strings 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.method to convert each element of a string array to a
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.
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