Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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 array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly.

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

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

Parameters

s

A string containing one or more dates and times to convert.

formats

An array of expected formats of s.

provider

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

style

A bitwise combination of DateTimeStyles values that indicates the permitted format of s. A typical value to specify is None.

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 s or formats is a null reference (Nothing in Visual Basic), s or an element of formats is an empty string, or the format of s is not exactly as specified by at least one of the format patterns in formats. This parameter is passed uninitialized.

Return Value

true if the s parameter 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 like the ParseExact method, except 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 formats parameter contains an array of patterns that correspond to the expected format of the s parameter. The patterns in the formats parameter consist 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 formats 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

Community Additions

ADD
Show:
© 2015 Microsoft