DateTime.ParseExact Method (String, String, IFormatProvider)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function ParseExact ( _
    s As String, _
    format As String, _
    provider As IFormatProvider _
) As DateTime
public static DateTime ParseExact(
    string s,
    string format,
    IFormatProvider provider
)

Parameters

  • s
    Type: System.String
    A string that contains a date and time to convert.
  • format
    Type: System.String
    A format specifier that defines the required format of s.

Return Value

Type: System.DateTime
An object that is equivalent to the date and time contained in the s parameter, as specified by the format and provider parameters.

Exceptions

Exception Condition
ArgumentNullException

s or format is nulla 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

The DateTime.ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the format parameter. It also requires that the <Date> and <Time> elements of the string representation of a date and time appear in the order specified by format, and that s have no white space other than that permitted by format. If format defines a date with no time element and the parse operation succeeds, the resulting DateTime value has a time of midnight (00:00:00). If format defines a time with no date element and the parse operation succeeds, the resulting DateTime value has a date of DateTime.Now.Date.

If s does not represent a time in a particular time zone and the parse operation succeeds, the Kind property of the returned DateTime value is DateTimeKind.Unspecified. If s does represent the time in a particular time zone and format allows time zone information to be present (for example, if format is equal to the o, r, or u standard format specifiers, or if it contains the z, zz, or zzz custom format specifiers), the Kind property of the returned DateTime value is DateTimeKind.Local.

The format parameter is a string that contains either a single standard format specifier, or one or more custom format specifiers that define the required format of s. For details about valid formatting codes, see Standard Date and Time Format Strings or Custom Date and Time Format Strings.

NoteNote:

If format is a custom format pattern that does not include date or time separators (such as "yyyyMMdd HHmm"), use the invariant culture for the provider parameter and the widest form of each custom format specifier. For example, if you want to specify hours in the format pattern, specify the wider form, "HH", instead of the narrower form, "H".

The particular date and time symbols and strings (such as names of the days of the week in a particular language) used in s are defined by the provider parameter, as is the precise format of s if format is a standard format specifier string. The provider parameter can be any of the following:

If provider is nulla null reference (Nothing in Visual Basic), the CultureInfo object that corresponds to the current culture is used.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 The ParseExact method accepts an incorrect time instead of throwing FormatException.

Examples

The following example demonstrates the ParseExact method.

Dim dateString, format As String
Dim result As Date
Dim provider As CultureInfo = CultureInfo.InvariantCulture

' Parse date-only value with invariant culture.
dateString = "06/15/2008"
format = "d"
Try
   result = Date.ParseExact(dateString, format, provider)
   outputBlock.Text += String.Format("{0} converts to {1}.", dateString, result.ToString()) & vbCrLf
Catch e As FormatException
   outputBlock.Text += String.Format("{0} is not in the correct format.", dateString) & vbCrLf
End Try

' Parse date-only value without leading zero in month using "d" format.
' Should throw a FormatException because standard short date pattern of 
' invariant culture requires two-digit month.
dateString = "6/15/2008"
Try
   result = Date.ParseExact(dateString, format, provider)
   outputBlock.Text += String.Format("{0} converts to {1}.", dateString, result.ToString()) & vbCrLf
Catch e As FormatException
   outputBlock.Text += String.Format("{0} is not in the correct format.", dateString) & vbCrLf
End Try

' Parse date and time with custom specifier.
dateString = "Sun 15 Jun 2008 8:30 AM -06:00"
format = "ddd dd MMM yyyy h:mm tt zzz"
Try
   result = Date.ParseExact(dateString, format, provider)
   outputBlock.Text += String.Format("{0} converts to {1}.", dateString, result.ToString()) & vbCrLf
Catch e As FormatException
   outputBlock.Text += String.Format("{0} is not in the correct format.", dateString) & vbCrLf
End Try

' Parse date and time with offset but without offset's minutes.
' Should throw a FormatException because "zzz" specifier requires leading  
' zero in hours.
dateString = "Sun 15 Jun 2008 8:30 AM -06"
Try
   result = Date.ParseExact(dateString, format, provider)
   outputBlock.Text += String.Format("{0} converts to {1}.", dateString, result.ToString()) & vbCrLf
Catch e As FormatException
   outputBlock.Text += String.Format("{0} is not in the correct format.", dateString) & vbCrLf
End Try

dateString = "15/06/2008 08:30"
format = "g"
provider = New CultureInfo("fr-FR")
Try
   result = Date.ParseExact(dateString, format, provider)
   outputBlock.Text += String.Format("{0} converts to {1}.", dateString, result.ToString()) & vbCrLf
Catch e As FormatException
   outputBlock.Text += String.Format("{0} is not in the correct format.", dateString) & vbCrLf
End Try
' The example displays the following output:
'       06/15/2008 converts to 6/15/2008 12:00:00 AM.
'       6/15/2008 is not in the correct format.
'       Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
'       Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
'       15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.
string dateString, format;
DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;

// Parse date-only value with invariant culture.
dateString = "06/15/2008";
format = "d";
try
{
   result = DateTime.ParseExact(dateString, format, provider);
   outputBlock.Text += String.Format("{0} converts to {1}.\n", dateString, result.ToString());
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} is not in the correct format.\n", dateString);
}

// Parse date-only value without leading zero in month using "d" format.
// Should throw a FormatException because standard short date pattern of 
// invariant culture requires two-digit month.
dateString = "6/15/2008";
try
{
   result = DateTime.ParseExact(dateString, format, provider);
   outputBlock.Text += String.Format("{0} converts to {1}.\n", dateString, result.ToString());
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} is not in the correct format.\n", dateString);
}

// Parse date and time with custom specifier.
dateString = "Sun 15 Jun 2008 8:30 AM -06:00";
format = "ddd dd MMM yyyy h:mm tt zzz";
try
{
   result = DateTime.ParseExact(dateString, format, provider);
   outputBlock.Text += String.Format("{0} converts to {1}.\n", dateString, result.ToString());
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} is not in the correct format.\n", dateString);
}

// Parse date and time with offset but without offset's minutes.
// Should throw a FormatException because "zzz" specifier requires leading  
// zero in hours.
dateString = "Sun 15 Jun 2008 8:30 AM -06";
try
{
   result = DateTime.ParseExact(dateString, format, provider);
   outputBlock.Text += String.Format("{0} converts to {1}.\n", dateString, result.ToString());
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} is not in the correct format.\n", dateString);
}

dateString = "15/06/2008 08:30";
format = "g";
provider = new CultureInfo("fr-FR");
try
{
   result = DateTime.ParseExact(dateString, format, provider);
   outputBlock.Text += String.Format("{0} converts to {1}.\n", dateString, result.ToString());
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} is not in the correct format.\n", dateString);
}
// The example displays the following output:
//       06/15/2008 converts to 6/15/2008 12:00:00 AM.
//       6/15/2008 is not in the correct format.
//       Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
//       Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
//       15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.