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.
[Visual Basic]
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.
[Visual Basic]
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.
[Visual Basic]
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.
[Visual Basic]
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);
Partager