Export (0) Print
Expand All

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

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
)
Not applicable.

Parameters

s

A string containing a date and time to convert.

provider

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

styles

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 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

ArgumentException

styles is not a valid DateTimeStyles value.

-or-

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 the TryParse method does not throw an exception if the conversion fails.

The string s is parsed using formatting information in the DateTimeFormatInfo object that is supplied either explicitly or implicitly 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.

Notes to Callers: Formatting is influenced by properties of the current DateTimeFormatInfo object, which is supplied by the provider parameter. One reason the TryParse method can unexpectedly throw FormatException is if the current DateTimeFormatInfo.DateSeparator and DateTimeFormatInfo.TimeSeparator properties are set to the same value.

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:
© 2014 Microsoft