DateTime::Parse Method (String)
Converts the specified string representation of a date and time to its DateTime equivalent.
Assembly: mscorlib (in mscorlib.dll)
The method tries to convert the string representation of a date and time value to its DateTime equivalent. The string to be parsed can take any of the following forms:
A string with a date and a time component.
A string with a date but no time component.
A string with a time but no date component.
A string that includes time zone information and conforms to ISO 8601. For example, the first of the following two strings designates the Coordinated Universal Time (UTC); the second designates the time in a time zone seven hours earlier than UTC:
A string that includes the GMT designator and conforms to the RFC 1123 time format. For example:
Sat, 01 Nov 2008 19:35:00 GMT
A string that includes the date and time along with time zone offset information. For example:
03/01/2009 05:42:00 -5:00
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. If s includes a date component with a two-digit year, it is converted to a year in the current culture's current calendar based on the value of the Calendar::TwoDigitYearMax property. 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).
Because the string representation of a date and time must conform to a recognized pattern, applications should always use exception handling when calling the method to parse user input. Alternatively, you can call the DateTime::TryParse(String, DateTime%) method to parse a date and time string and return a value that indicates whether the parse operation succeeded.
The string s is parsed using formatting information in the current DateTimeFormatInfo object, which is supplied implicitly by the current thread culture.
Because the method tries to parse the string representation of a date and time using the formatting rules of the current culture, 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 the DateTime::Parse(String, IFormatProvider) method or one of the overloads of the ParseExact method and provide a format specifier.
In most cases, the 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 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. The following example illustrates these string representations.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 Parse method can unexpectedly throw FormatException is if the current DateTimeFormatInfo::DateSeparator and DateTimeFormatInfo::TimeSeparator properties are set to the same value.
The following example demonstrates the method. It parses the string representation of several date and time values using the formatting conventions of the en-US culture, which is the current thread culture of the computer used to produce the example output. It handles the FormatException that is thrown when the method tries to parse the string representation of a date and time using some other culture's formatting conventions. It also shows how to successfully parse a date and time value that does not use the formatting conventions of the current thread culture.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.