DateTime::ParseExact Method (String^, String^, IFormatProvider^)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

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

static DateTime ParseExact(
	String^ s,
	String^ format,
	IFormatProvider^ provider


Type: System::String^

A string that contains a date and time to convert.

Type: System::String^

A format specifier that defines the required format of s. For more information, see the Remarks section.

Type: System::IFormatProvider^

An object that supplies culture-specific format information about s.

Return Value

Type: System::DateTime

An object that is equivalent to the date and time contained in s, as specified by format and provider.

Exception Condition

s or format is null.


s or format is an empty string.


s does not contain a date and time that corresponds to the pattern specified in format.


The hour component and the AM/PM designator in s do not agree.

The DateTime::ParseExact(String^, String^, IFormatProvider^) method parses the string representation of a date, which must be in the format defined by the format parameter. It also requires that the <Date> and <Time> elements of the string representation of a date and time appear in the order specified by format, and that s have no white space other than that permitted by format. If format defines a date with no time element and the parse operation succeeds, the resulting DateTime value has a time of midnight (00:00:00). If format defines a time with no date element and the parse operation succeeds, the resulting DateTime value has a date of DateTime.Now.Date.

If s does not represent a time in a particular time zone and the parse operation succeeds, theKind property of the returned DateTime value is DateTimeKind::Unspecified. If s does represent the time in a particular time zone and format allows time zone information to be present (for example, if format is equal to the "o", "r", or "u" standard format specifiers, or if it contains the "z", "zz", or "zzz" custom format specifiers), the Kind property of the returned DateTime value is DateTimeKind::Local.

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 s. For details about valid formatting codes, see Standard Date and Time Format Strings or Custom Date and Time Format Strings.


If format is a custom format pattern that does not include date or time separators (such as "yyyyMMddHHmm"), use the invariant culture for the provider parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H".

The particular date and time symbols and strings (such as names of the days of the week in a particular language) used in s are defined by the provider parameter, as is the precise format of s if format is a standard format specifier string. The provider parameter can be any of the following:

If provider is null, the CultureInfo object that corresponds to the current culture is used.

Notes to Callers:

In the .NET Framework 4, the ParseExact method throws a FormatException if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. In the .NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.

The following example demonstrates the ParseExact method.

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

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