DateTime.Parse Method
Converts the specified string representation of a date and time to its DateTime equivalent.
Overload List
Converts the specified string representation of a date and time to its DateTime equivalent.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function Parse(String) As DateTime
[C#] public static DateTime Parse(string);
[C++] public: static DateTime Parse(String*);
[JScript] public static function Parse(String) : DateTime;
Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function Parse(String, IFormatProvider) As DateTime
[C#] public static DateTime Parse(string, IFormatProvider);
[C++] public: static DateTime Parse(String*, IFormatProvider*);
[JScript] public static function Parse(String, IFormatProvider) : DateTime;
Converts the specified string representation of a date and time to its DateTime equivalent using the specified culture-specific format information and formatting style.
Supported by the .NET Compact Framework.
[Visual Basic] Overloads Public Shared Function Parse(String, IFormatProvider, DateTimeStyles) As DateTime
[C#] public static DateTime Parse(string, IFormatProvider, DateTimeStyles);
[C++] public: static DateTime Parse(String*, IFormatProvider*, DateTimeStyles);
[JScript] public static function Parse(String, IFormatProvider, DateTimeStyles) : DateTime;
Example
[Visual Basic, C#, C++] The following sample demonstrates the Parse method.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of Parse. For other examples that might be available, see the individual overload topics.
[Visual Basic] Imports System Imports System.Globalization Class Class1 Public Shared Sub Main() ' Assume the current culture is en-US. ' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. Dim myDateTimeValue As String = "2/16/1992 12:15:12" Dim myDateTime As DateTime = DateTime.Parse(myDateTimeValue) Console.WriteLine("1) myDateTime = {0}", myDateTime) ' Reverse month and day to conform to a different culture. ' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. Dim culture = New CultureInfo("fr-FR", True) Dim myDateTimeFrenchValue As String = " 16/02/1992 12:15:12" Dim myDateTimeFrench As DateTime = _ DateTime.Parse(myDateTimeFrenchValue, _ culture, _ DateTimeStyles.NoCurrentDateDefault) Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench) ' The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. Dim expectedFormats As String() = {"G", "g", "f", "F"} myDateTimeFrench = DateTime.ParseExact(myDateTimeFrenchValue, _ expectedFormats, _ culture, _ DateTimeStyles.AllowWhiteSpaces) Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench) End Sub 'Main End Class 'Class1 ' 'This example yields the following results: ' '1) myDateTime = 2/16/1992 12:15:12 PM '2) myDateTimeFrench = 2/16/1992 12:15:12 PM '3) myDateTimeFrench = 2/16/1992 12:15:12 PM ' [C#] using System; using System.Globalization; namespace Parse { class Class1 { public static void Main(string[] args) { // Assume the current culture is en-US. // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. string myDateTimeValue = "2/16/1992 12:15:12"; DateTime myDateTime = DateTime.Parse(myDateTimeValue); Console.WriteLine("1) myDateTime = {0}", myDateTime); // Reverse month and day to conform to a different culture. // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. IFormatProvider culture = new CultureInfo("fr-FR", true); string myDateTimeFrenchValue = " 16/02/1992 12:15:12"; DateTime myDateTimeFrench = DateTime.Parse(myDateTimeFrenchValue, culture, DateTimeStyles.NoCurrentDateDefault); Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench); // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. string[] expectedFormats = {"G", "g", "f" ,"F"}; myDateTimeFrench = DateTime.ParseExact(myDateTimeFrenchValue, expectedFormats, culture, DateTimeStyles.AllowWhiteSpaces); Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench); } } } /* This example yields the following results: 1) myDateTime = 2/16/1992 12:15:12 PM 2) myDateTimeFrench = 2/16/1992 12:15:12 PM 3) myDateTimeFrench = 2/16/1992 12:15:12 PM */ [C++] #using <mscorlib.dll> using namespace System; using namespace System::Globalization; int main() { // Assume the current culture is en-US. // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. String* myDateTimeValue = S"2/16/1992 12:15:12"; DateTime myDateTime = DateTime::Parse(myDateTimeValue); Console::WriteLine(S"1) myDateTime = {0}", __box(myDateTime)); // Reverse month and day to conform to a different culture. // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. IFormatProvider* culture = new CultureInfo(S"fr-FR", true); String* myDateTimeFrenchValue = S" 16/02/1992 12:15:12"; DateTime myDateTimeFrench = DateTime::Parse(myDateTimeFrenchValue, culture, DateTimeStyles::NoCurrentDateDefault); Console::WriteLine(S"2) myDateTimeFrench = {0}", __box(myDateTimeFrench)); // The date is Feburary 16, 1992, 12 hours, 15 minutes and 12 seconds. String* expectedFormats[] = {S"G", S"g", S"f" , S"F"}; myDateTimeFrench = DateTime::ParseExact(myDateTimeFrenchValue, expectedFormats, culture, DateTimeStyles::AllowWhiteSpaces); Console::WriteLine(S"3) myDateTimeFrench = {0}", __box(myDateTimeFrench)); } /* This example yields the following results: 1) myDateTime = 2/16/1992 12:15:12 PM 2) myDateTimeFrench = 2/16/1992 12:15:12 PM 3) myDateTimeFrench = 2/16/1992 12:15:12 PM */
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.