Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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

Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information, and styles. The format of the string representation must match the specified format exactly.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
static member ParseExact : 
        input:string * 
        format:string * 
        formatProvider:IFormatProvider * 
        styles:TimeSpanStyles -> TimeSpan

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

A bitwise combination of enumeration values that defines the style elements that may be present in input.

Return Value

Type: System.TimeSpan
A time interval that corresponds to input, as specified by format, formatProvider, and styles.
ExceptionCondition
ArgumentException

styles is an invalid TimeSpanStyles value.

ArgumentNullException

input is a null reference (Nothing in Visual Basic).

FormatException

input has an invalid format.

OverflowException

input represents a number that is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.

-or-

At least one of the days, hours, minutes, or seconds components in input is outside its valid range.

The ParseExact 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. Because input must conform to the format of format exactly, 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, TimeSpanStyles, TimeSpan) method instead.

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.

Important noteImportant

The ParseExact 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 a null reference (Nothing in Visual Basic), the DateTimeFormatInfo object that is associated with the current culture is used.

The styles parameter affects the interpretation of strings that are 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.

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

.NET Framework

Supported in: 4.5.1, 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 8, Silverlight 8.1

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.