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


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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)

static member TryParse : 
        s:string *
        provider:IFormatProvider *
        styles:DateTimeStyles *
        result:DateTime byref -> bool


Type: System.String

A string containing a date and time to convert.

Type: System.IFormatProvider

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

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.

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 null, 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.

Exception Condition

styles is not a valid DateTimeStyles value.


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


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



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.


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


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


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


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.


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.


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.


Although valid, this value is ignored.


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.

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.

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top