Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DateTime.ParseExact Method

Converts the specified string representation of a date and time to its DateTime equivalent. The format of the string representation must match a specified format exactly.

Overload List

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ParseExact(String, String, IFormatProvider) As DateTime
[C#] public static DateTime ParseExact(string, string, IFormatProvider);
[C++] public: static DateTime ParseExact(String*, String*, IFormatProvider*);
[JScript] public static function ParseExact(String, String, IFormatProvider) : DateTime;

Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ParseExact(String, String, IFormatProvider, DateTimeStyles) As DateTime
[C#] public static DateTime ParseExact(string, string, IFormatProvider, DateTimeStyles);
[C++] public: static DateTime ParseExact(String*, String*, IFormatProvider*, DateTimeStyles);
[JScript] public static function ParseExact(String, String, IFormatProvider, DateTimeStyles) : DateTime;

Converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function ParseExact(String, String(), IFormatProvider, DateTimeStyles) As DateTime
[C#] public static DateTime ParseExact(string, string[], IFormatProvider, DateTimeStyles);
[C++] public: static DateTime ParseExact(String*, String*[], IFormatProvider*, DateTimeStyles);
[JScript] public static function ParseExact(String, String[], IFormatProvider, DateTimeStyles) : DateTime;

Example

[Visual Basic, C#, C++] The following sample demonstrates the ParseExact method.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of ParseExact. 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 Language Filter in the upper-left corner of the page.

See Also

DateTime Structure | DateTime Members | System Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft