Export (0) Print
Expand All

Parsing Date and Time Strings 

Parsing methods convert the string representation of a date and time to an equivalent DateTime object. The Parse and TryParse methods convert any of several common representations of a date and time. The ParseExact and TryParseExact methods convert a string representation that complies exactly with the pattern specified by a date and time format string.

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 current DateTimeFormatInfo object, which is provided implicitly by the current thread culture or explicitly by the IFormatProvider parameter of a parsing method. For the IFormatProvider parameter, specify a CultureInfo 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 Year, Month, and Day properties set to the corresponding values of the Today property. However, if the NoCurrentDateDefault 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 its Kind property set to Unspecified. 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 Local and its value adjusted to the local time zone of your machine. You can modify this behavior by using a DateTimeStyles 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.

Parse

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.

using System.Globalization;

CultureInfo MyCultureInfo = new CultureInfo("de-DE");
string MyString = "12 Juni 2002";
DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo, DateTimeStyles.NoCurrentDateDefault);
Console.WriteLine(MyDateTime);

ParseExact

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.

using System.Globalization;

CultureInfo MyCultureInfo = new CultureInfo("en-US");
string MyString = " Tuesday, April 10, 2001";
DateTime MyDateTime = DateTime.ParseExact(MyString, "D", MyCultureInfo);
Console.WriteLine(MyDateTime);

See Also

Community Additions

ADD
Show:
© 2014 Microsoft