Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

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

Note: This method is new in the .NET Framework version 2.0.

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
)
JScript does not support passing value-type arguments by reference.

Parameters

s

A string containing a date and time to convert.

format

The expected format of s.

provider

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

style

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

result

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

ArgumentException

styles is not a valid DateTimeStyles value.

-or-

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 this 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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft