Export (0) Print
Expand All

DateTime.TryParse Method (String, DateTime)

Converts the specified string representation of a date and time to its DateTime equivalent.

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

public static bool TryParse (
	string s,
	out DateTime result
)
public static boolean TryParse (
	String s, 
	/** @attribute OutAttribute() */ /** @ref */ DateTime result
)
Not applicable.

Parameters

s

A string containing a date and time to convert.

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.

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 current DateTimeFormatInfo object, which is supplied implicitly by the current thread culture.

This method attempts to ignore unrecognized data and parse s completely. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current time. If s contains only a date and no time, this method assumes the time is 12:00 midnight. 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.

Notes to Callers: Formatting is influenced by properties of the current DateTimeFormatInfo object, which by default are derived from the Regional and Language Options item in Control Panel. 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