Partager via


DateTimeOffset.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 DateTimeOffset 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 ( _
    input As String, _
    format As String, _
    formatProvider As IFormatProvider _
) As DateTimeOffset
public static DateTimeOffset ParseExact(
    string input,
    string format,
    IFormatProvider formatProvider
)

Parameters

  • input
    Type: System.String
    A string that contains a date and time to convert.
  • format
    Type: System.String
    A format specifier that defines the expected format of input.
  • formatProvider
    Type: System.IFormatProvider
    An object that supplies culture-specific formatting information about input.

Return Value

Type: System.DateTimeOffset
An object that is equivalent to the date and time that is contained in input, as specified by format and formatProvider.

Exceptions

Exception Condition
ArgumentException

The offset is greater than 14 hours or less than -14 hours.

ArgumentNullException

input is nulla null reference (Nothing in Visual Basic).

-or-

format is nulla null reference (Nothing in Visual Basic).

FormatException

input is an empty string ("").

-or-

input does not contain a valid string representation of a date and time.

-or-

format is an empty string.

Remarks

The 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>, <Time>, and <Offset> elements of the string representation of a date and time appear in the order specified by format. If the input string does not match this format parameter, the method throws a FormatException. In contrast, the DateTimeOffset.Parse(String, IFormatProvider) method parses the string representation of a date in any one of the formats recognized by the formatProvider parameter's DateTimeFormatInfo object. Parse also allows the <Date>, <Time>, and <Offset> elements of the string representation of a date and time to appear in any order.

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 the input parameter. For details about valid formatting codes, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. If format includes the z, zz, or zzz custom format specifiers to indicate that an offset must be present in input, that offset must include either a negative sign or a positive sign. If the sign is missing, the method throws a FormatException.

If format requires that input contain a date but not a time, the resulting DateTimeOffset object is assigned a time of midnight (0:00:00). If format requires that input contain a time but not a date, the resulting DateTimeOffset object is assigned the current date on the local system. If format does not require that input contain an offset, the resulting DateTimeOffset object is assigned the time zone offset of the local system.

The particular date and time symbols and strings used in input are defined by the formatProvider parameter, as is the precise format of input if format is a standard format specifier string. The formatProvider parameter can be either of the following:

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

Examples

The following example uses the DateTimeOffset.ParseExact(String, String, IFormatProvider) method with standard and custom format specifiers and the invariant culture to parse several date and time strings.

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

' Parse date-only value with invariant culture.
dateString = "06/15/2008"
format = "d"
Try
   result = DateTimeOffset.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 = DateTimeOffset.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 = DateTimeOffset.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 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 = DateTimeOffset.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 -07:00.
'    6/15/2008 is not in the correct format.
'    Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 8:30:00 AM -06:00.
'    Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.                     
string dateString, format;
DateTimeOffset result;
CultureInfo provider = CultureInfo.InvariantCulture;

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

// 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 = DateTimeOffset.ParseExact(dateString, format, provider);
   outputBlock.Text += String.Format("{0} converts to {1}.", dateString, result.ToString()) + "\n";
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} is not in the correct format.", dateString) + "\n";
}

// 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 = DateTimeOffset.ParseExact(dateString, format, provider);
   outputBlock.Text += String.Format("{0} converts to {1}.", dateString, result.ToString()) + "\n";
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} is not in the correct format.", dateString) + "\n";
}

// Parse date and time with offset 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 = DateTimeOffset.ParseExact(dateString, format, provider);
   outputBlock.Text += String.Format("{0} converts to {1}.", dateString, result.ToString()) + "\n";
}
catch (FormatException)
{
   outputBlock.Text += String.Format("{0} is not in the correct format.", dateString) + "\n";
}
// The example displays the following output:
//    06/15/2008 converts to 6/15/2008 12:00:00 AM -07:00.
//    6/15/2008 is not in the correct format.
//    Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 8:30:00 AM -06:00.
//    Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.                     

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.