DateTime.TryParse Method (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 culture-specific format information and formatting style.

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

public static bool TryParse (
	string s,
	IFormatProvider provider,
	DateTimeStyles styles,
	out DateTime result
public static boolean TryParse (
	String s, 
	IFormatProvider provider, 
	DateTimeStyles styles, 
	/** @attribute OutAttribute() */ /** @ref */ DateTime result
JScript does not support passing value-type arguments by reference.



A string containing a date and time to convert.


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


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


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 the s parameter is a null reference (Nothing in Visual Basic), or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.

Return Value

true if the s parameter 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 TryParse method is similar to the Parse method, except that this method does not throw an exception if the conversion fails.

The string s is parsed using the formatting information in a DateTimeFormatInfo object supplied by the provider parameter.

This method attempts to ignore unrecognized data and parse s completely. If s contains a time but no date, the style parameter determines whether the current date or a default date is used. If s contains a date but no time, 12:00 midnight is used as the default time. Any leading, inner, or trailing white space character in s is ignored. The date and time can be bracketed with a pair of leading and trailing NUMBER SIGN characters ('#', U+0023), and can be trailed with one or more NULL characters (U+0000). The s parameter must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo class.

The provider parameter supplies culture-specific date and time formatting information. For example, it might supply the names of the days of the week in a particular language, or the preferred order of presentation for the month, day, and year. If provider is a null reference (Nothing in Visual Basic), the current culture is used.

Parsing Custom Cultures

If you parse a date and time string generated for a custom culture, use the TryParseExact method instead of the TryParse method to improve the probability that the parse operation will succeed. A custom culture date and time string can be complicated and difficult to parse. The TryParse method attempts to parse a string with several implicit parse patterns, all of which might fail. In contrast, the TryParseExact method requires you to explicitly designate one or more exact parse patterns that are likely to succeed.

For more information about custom cultures, see the System.Globalization.CultureAndRegionInfoBuilder class.

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

Community Additions