DateTime.TryParse Method (String, IFormatProvider, DateTimeStyles, DateTime)
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 )
A string containing a date and time to convert.
An IFormatProvider object that supplies culture-specific formatting information about 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 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 Valuetrue if the s parameter was converted successfully; otherwise, false.
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.
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 EditionThe Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.