Parsing Date and Time Strings
Parsing methods convert the string representation of a date and time to an equivalent date and time format string.object. The and methods convert any of several common representations of a date and time. The and methods convert a string representation that complies exactly with the pattern specified by a
Parsing is influenced by the properties of a format provider that supplies information such as the strings used for date and time separators, and the names of months, days, and eras. The format provider is the currentobject, which is provided implicitly by the current thread culture or explicitly by the parameter of a parsing method. For the IFormatProvider parameter, specify a object, which represents a culture, or a DateTimeFormatInfo object.
The string representation of a date to be parsed must include the month and at least a day or year. The string representation of a time must include the hour and at least minutes or the AM/PM designator. However, parsing supplies default values for omitted components if possible. A missing date defaults to the current date, a missing year defaults to the current year, a missing day of the month defaults to the first day of the month, and a missing time defaults to midnight.
If the string representation specifies only a time, parsing returns a DateTime object with its, , and properties set to the corresponding values of the property. However, if the constant is specified in the parsing method, the resulting year, month, and day properties are set to the value 1.
If the time zone is omitted from the string representation of a time, parsing returns a DateTime object with itsproperty set to . If the time zone is specified, for example by the time zone offset "-07:00", parsing returns a DateTime object with its Kind property set to and its value adjusted to the local time zone of your machine. You can modify this behavior by using a constant with the parsing method.
The format provider is also used to interpret an ambiguous numeric date. For example, it is not clear which components of the date represented by the string "02/03/04" are the month, day, and year. In this case, the components are interpreted according to the order of similar date formats in the format provider.
The following code example illustrates the use of the Parse method to convert a string into a DateTime. This example uses the culture associated with the current thread to perform the parse. If the CultureInfo associated with the current culture cannot parse the input string, a FormatException is thrown.
string MyString = "Jan 1, 2002"; DateTime MyDateTime = DateTime.Parse(MyString); Console.WriteLine(MyDateTime);
You can also specify a CultureInfo set to one of the cultures defined by that object. The following code example uses a format provider to parse a German string into a DateTime. A CultureInfo representing the de-DE culture is defined and passed with the string being parsed to ensure successful parsing of this particular string. This precludes whatever setting is in the CurrentCulture of the CurrentThread.
using System.Globalization; CultureInfo MyCultureInfo = new CultureInfo("de-DE"); string MyString = "12 Juni 2002"; DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo); Console.WriteLine(MyDateTime);
The following code example uses the DateTimeStyles enumeration to specify that the current date and time information should not be added to the DateTime for fields that the string does not define.
The ParseExact method only converts the specified string pattern to a DateTime. When a string that is not of the form specified is passed to this method, a FormatException is thrown. You can specify one of the standard date and time format specifiers or a limited combination of the custom date and time format specifiers. Using the custom format specifiers, it is possible for you to construct a custom recognition string. For an explanation of the specifiers, see the section on date and time format strings.
In the following code example, the ParseExact method is passed a string object to parse, followed by a format specifier, followed by a CultureInfo object. This ParseExact method can only parse strings that exhibit the long date pattern in the en-US culture.