Export (0) Print
Expand All

DateTime.ParseExact Method (String, String, IFormatProvider, DateTimeStyles)

.NET Framework 1.1

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.

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

Parameters

s
A string containing a date and time to convert.
format
The expected format of s.
provider
An IFormatProvider that supplies culture-specific formatting information about s.
style
The combination of one or more DateTimeStyles constants that indicate the permitted format of s.

Return Value

A DateTime equivalent to the date and time contained in s as specified by format, provider, and style.

Exceptions

Exception Type Condition
ArgumentNullException s or format is a null reference (Nothing in Visual Basic).
FormatException s or format is an empty string

-or-

s does not contain a date and time that corresponds to the pattern specified in format.

Remarks

This method throws FormatException if the format of s is not exactly as specified by the format pattern in format. If format consists of a single standard format character, the format pattern that character represents is used. For more information, see the DateTimeFormatInfo topic.

If s contains a time but no date, the style parameter determines whether the current date or a default date is used. If s contains a date but no time, 12 A.M. is used. The style parameter also determines whether leading, inner, or trailing white space characters are ignored.

Parameter provider supplies culture-specific date and time formatting information. For example, the names of the days of the week in a particular language, or the preferred presentation order of the month, day, and year. If provider is a null reference (Nothing in Visual Basic), the current culture is used.

Example

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

[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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

DateTime Structure | DateTime Members | System Namespace | DateTime.ParseExact Overload List | Formatting Overview | Date and Time Format Strings | Parse | String | CultureInfo | CurrentInfo

Show:
© 2014 Microsoft