DateTime.TryParseExact Method (String, String, IFormatProvider, DateTimeStyles, DateTime)

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

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

public static bool TryParseExact (
	string s,
	string format,
	IFormatProvider provider,
	DateTimeStyles style,
	out DateTime result
public static boolean TryParseExact (
	String s, 
	String format, 
	IFormatProvider provider, 
	DateTimeStyles style, 
	/** @attribute OutAttribute() */ /** @ref */ DateTime result
Not applicable.



A string containing a date and time to convert.


The expected format of s.


An IFormatProvider object that supplies culture-specific formatting information about s.


A bitwise combination of one or more DateTimeStyles values that indicate the permitted format of s.


When this method returns, contains the DateTime value equivalent to the date and time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if either the s or format parameter is a null reference (Nothing in Visual Basic), is an empty string, or does not contain a date and time that correspond to the pattern specified in format. This parameter is passed uninitialized.

Return Value

true if s was converted successfully; otherwise, false.

Exception typeCondition


styles is not a valid DateTimeStyles value.


styles contains an invalid combination of DateTimeStyles values (for example, both AssumeLocal and AssumeUniversal).

The TryParseExact method is similar to the ParseExact method, except that the TryParseExact method does not throw an exception if the conversion fails.

The s parameter contains the date and time to parse. If the s parameter contains only a time and no date, the style parameter determines whether the current date or a default date is used. If the s parameter contains only a date and no time, midnight (00:00:00) is used. The style parameter also determines whether the s parameter can contain leading, inner, or trailing white space characters.

The format parameter contains a pattern that corresponds to the expected format of the s parameter. The pattern in the format parameter consists of one or more custom format specifiers from the Custom DateTime Format Strings table, or a single standard format specifier, which identifies a predefined pattern, from the Standard DateTime Format Strings table.

If you do not use date or time separators in a custom format pattern, 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 pattern, specify the wider form, "HH", instead of the narrower form, "H".

The provider parameter supplies culture-specific date and time formatting information, such as the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. The format parameter is typically a culture represented by a CultureInfo object. If provider is a null reference (Nothing in Visual Basic), the current culture is used.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0