Share via


Parsing Date and Time Strings

Similar to the numeric types, the DateTime class has the capability to convert a string into a DateTime object. Both the Parse method and the ParseExact method can be used to convert a string representation of a date or time into a DateTime object. The Parse method converts any valid string representation, while the ParseExact method only converts strings that are of the form that you specify. Both methods can successfully convert any string that takes the form of one of the standard DateTime patterns described in the Date and Time Format Strings section.

The values used to represent the names of months and days, as well as the display order of DateTime components, are defined in format providers. Both the Parse and the ParseExact methods accept a format provider, allowing you to specify and explicitly parse culture-specific strings. If no format provider is specified, then the provider associated with the current thread is used. For more information, see Formatting Overview.

By default, any information about the date or time that is not contained in the passed string is filled in with the current date and time information from DateTime.Now. For example, if you parse the string "1/1/00", only the Month, Year, and Day properties of the DateTime are specified. Other properties like Minutes, Seconds, and Ticks are set to the current values as specified by DateTime.Now. This behavior can be modified by specifying the DateTimeStyles.NoCurrentDateDefault, which causes the Year, Month, and Day to each be set to "1" instead of the current year, month, and day.

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.

Dim MyString As String = "Jan 1, 2002"
Dim MyDateTime As DateTime = DateTime.Parse(MyString)
Console.WriteLine(MyDateTime)
[C#]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.

Imports System.Globalization

Dim MyCultureInfo As CultureInfo = new CultureInfo("de-DE")
Dim MyString As String = "12 Juni 2002"
Dim MyDateTime As DateTime = DateTime.Parse(MyString, MyCultureInfo)
Console.WriteLine(MyDateTime)
[C#]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.

Imports System.Globalization

Dim MyCultureInfo As CultureInfo = new CultureInfo("de-DE")
Dim MyString As String = "12 Juni 2002"
Dim MyDateTime As DateTime = DateTime.Parse(MyString, MyCultureInfo, DateTimeStyles.NoCurrentDateDefault)
Console.WriteLine(MyDateTime)
[C#]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.

Imports System.Globalization

Dim MyCultureInfo As CultureInfo = new CultureInfo("en-US")
Dim MyString As String = "Tuesday, April 10, 2001"
Dim MyDateTime As DateTime = DateTime.ParseExact(MyString, "D", MyCultureInfo)
Console.WriteLine(MyDateTime)
[C#]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

Parsing Strings | Formatting Types | Converting Types