The string s is parsed using formatting information in the DateTimeFormatInfo object that is supplied either explicitly or implicitly by the provider parameter. The s parameter must contain the representation of a date and time in one of the formats returned by the DateTimeFormatInfo..::.GetAllDateTimePatterns()()() method of the culture specified by provider.
Important Note: |
|---|
Because the string representation of a date and time must conform to a recognized pattern, applications should always use exception handling when calling the Parse(String) method to parse user input. |
This method attempts to parse s completely and avoid throwing a FormatException. It ignores unrecognized data if possible and fills in missing month, day, and year information with the current date. If s contains only a date and no time, this method assumes 12:00 midnight. If s contains only a time and no date, this method assumes the current date. 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 format of s is defined by the provider parameter, which can be any of the following:
If provider is nullNothingnullptra null reference (Nothing in Visual Basic), the current culture is used.
In most cases, the Parse(String) method returns a DateTime value whose Kind property is DateTimeKind..::.Unspecified. However, if the string to be parsed contains time zone information as defined by ISO 8601 or if it includes the older GMT designator, the Parse(String) method performs any necessary time conversion and returns a DateTime value whose date and time reflects the local time and whose Kind property is DateTimeKind..::.Local.
Because the DateTime..::.Parse(String, IFormatProvider) method tries to parse the string representation of a date and time using the formatting rules of the provider parameter, trying to parse a particular string across different cultures can either fail or return different results. If a specific date and time format will be parsed across different locales, use one of the overloads of the ParseExact method and provide a format specifier.
Parsing Non-Standard Cultures
If you parse a date and time string using a CultureInfo or DateTimeFormatInfo object which includes customized settings that are different from those of a standard culture, use the ParseExact method instead of the Parse method to improve the probability that the parse operation will succeed. A non-standard date and time string can be complicated and difficult to parse. The Parse method tries to parse a string with several implicit parse patterns, all of which might fail. In contrast, the ParseExact method requires you to explicitly designate one or more exact parse patterns that are likely to succeed.
For more information about custom or non-standard cultures, see the System.Globalization..::.CultureAndRegionInfoBuilder class.
Notes to Callers: Formatting is influenced by properties of the DateTimeFormatInfo object that is supplied by the provider parameter. One reason the Parse method can unexpectedly throw FormatException is if the current DateTimeFormatInfo..::.DateSeparator and DateTimeFormatInfo..::.TimeSeparator properties are set to the same value.