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, and returns a value that indicates whether the conversion succeeded.

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

public:
static bool TryParse(
	String^ s, 
	IFormatProvider^ provider, 
	DateTimeStyles styles, 
	[OutAttribute] DateTime% result
)

Parameters

s
Type: System::String

A string containing a date and time to convert.

provider
Type: System::IFormatProvider

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

styles
Type: System.Globalization::DateTimeStyles

A bitwise combination of enumeration values that defines how to interpret the parsed date in relation to the current time zone or the current date. A typical value to specify is None.

result
Type: System::DateTime%

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 nullptr, is an empty string (""), or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.

Return Value

Type: System::Boolean
true if the s parameter was converted successfully; otherwise, false.

ExceptionCondition
ArgumentException

styles is not a valid DateTimeStyles value.

-or-

styles contains an invalid combination of DateTimeStyles values (for example, both AssumeLocal and AssumeUniversal).

NotSupportedException

provider is a neutral culture and cannot be used in a parsing operation.

The DateTime::TryParse(String, IFormatProvider, DateTimeStyles, DateTime%) method parses a string that can contain date, time, and time zone information. It is similar to the DateTime::Parse(String, IFormatProvider, DateTimeStyles) method, except that the DateTime::TryParse(String, DateTime%) method does not throw an exception if the conversion fails.

This method attempts to ignore unrecognized data and parse s completely. If s contains a time but no date, the method by default substitutes the current date or, if styles includes the NoCurrentDateDefault flag, it substitutes DateTime.Date.MinValue. If s contains a date but no time, 12:00 midnight is used as the default time. If a date is present but its year component consists of only two digits, it is converted to a year in the provider parameter's current calendar based on the value of the Calendar::TwoDigitYearMax property. Any leading, inner, or trailing white space characters in s are 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).

Specific valid formats for date and time elements, as well as the names and symbols used in dates and times, are defined by the provider parameter, which can be any of the following:

If provider is nullptr, the current culture is used.

If s is the string representation of a leap day in a leap year in the current calendar, the method parses s successfully. If s is the string representation of a leap day in a non-leap year in the current calendar of provider, the parse operation fails and the method returns false.

The styles parameter defines the precise interpretation of the parsed string and how the parse operation should handle it. It can be one or more members of the DateTimeStyles enumeration, as described in the following table.

DateTimeStyles member

Description

AdjustToUniversal

Parses s and, if necessary, converts it to UTC. If s includes a time zone offset, or if s contains no time zone information but styles includes the DateTimeStyles::AssumeLocal flag, the method parses the string, calls ToUniversalTime to convert the returned DateTime value to UTC, and sets the Kind property to DateTimeKind::Utc. If s indicates that it represents UTC, or if s does not contain time zone information but styles includes the DateTimeStyles::AssumeUniversal flag, the method parses the string, performs no time zone conversion on the returned DateTime value, and sets the Kind property to DateTimeKind::Utc. In all other cases, the flag has no effect.

AllowInnerWhite

Although valid, this value is ignored. Inner white space is permitted in the date and time elements of s.

AllowLeadingWhite

Although valid, this value is ignored. Leading white space is permitted in the date and time elements of s.

AllowTrailingWhite

Although valid, this value is ignored. Trailing white space is permitted in the date and time elements of s.

AllowWhiteSpaces

Specifies that s may contain leading, inner, and trailing white spaces. This is the default behavior. It cannot be overridden by supplying a more restrictive DateTimeStyles enumeration value such as DateTimeStyles::None.

AssumeLocal

Specifies that if s lacks any time zone information, it is assumed to represent a local time. Unless the DateTimeStyles::AdjustToUniversal flag is present, the Kind property of the returned DateTime value is set to DateTimeKind::Local.

AssumeUniversal

Specifies that if s lacks any time zone information, it is assumed to represent UTC. Unless the DateTimeStyles::AdjustToUniversal flag is present, the method converts the returned DateTime value from UTC to local time and sets its Kind property to DateTimeKind::Local.

None

Although valid, this value is ignored.

RoundtripKind

For strings that contain time zone information, tries to prevent the conversion of a date and time string to a DateTime value with its Kind property set to DateTimeKind::Local. Typically, such a string is created by calling the DateTime::ToString(String) method using either the "o", "r", or "u" standard format specifiers.

If s contains no time zone information, the DateTime::TryParse(String, IFormatProvider, DateTimeStyles, DateTime%) method returns a DateTime value whose Kind property is DateTimeKind::Unspecified unless a styles flag indicates otherwise. If s includes time zone or time zone offset information, the DateTime::TryParse(String, IFormatProvider, DateTimeStyles, DateTime%) method performs any necessary time conversion and returns one of the following:

This behavior can be overridden by using the DateTimeStyles::RoundtripKind flag.

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. The TryParse method can unexpectedly fail and return False if the current DateTimeFormatInfo::DateSeparator and DateTimeFormatInfo::TimeSeparator properties are set to the same value.

The following example illustrates the DateTime::TryParse(String, IFormatProvider, DateTimeStyles, DateTime%) method.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft