Поделиться через


DateTimeOffset.TryParseExact Method (String, String, IFormatProvider, DateTimeStyles, DateTimeOffset%)

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, culture-specific format information, and style. The format of the string representation must match the specified format exactly.

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

Syntax

'Declaration
Public Shared Function TryParseExact ( _
    input As String, _
    format As String, _
    formatProvider As IFormatProvider, _
    styles As DateTimeStyles, _
    <OutAttribute> ByRef result As DateTimeOffset _
) As Boolean
public static bool TryParseExact(
    string input,
    string format,
    IFormatProvider formatProvider,
    DateTimeStyles styles,
    out DateTimeOffset result
)

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 required format of input.
  • formatProvider
    Type: System.IFormatProvider
    An object that supplies culture-specific formatting information about input.
  • styles
    Type: System.Globalization.DateTimeStyles
    A bitwise combination of enumeration values that indicates the permitted format of input. A typical value to specify is None.
  • result
    Type: System.DateTimeOffset%
    When the method returns, contains the DateTimeOffset equivalent to the date and time of input, if the conversion succeeded, or MinValue, if the conversion failed. The conversion fails if the input parameter is nulla null reference (Nothing in Visual Basic), or does not contain a valid string representation of a date and time in the expected format defined by format and provider. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if the input parameter is successfully converted; otherwise, false.

Exceptions

Exception Condition
ArgumentException

styles includes an undefined DateTimeStyles value.

-or-

DateTimeStyles.NoCurrentDateDefault is not supported.

-or-

styles includes mutually exclusive DateTimeStyles values.

Remarks

This overload of the TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset%) method is like the DateTimeOffset.ParseExact(String, String, IFormatProvider, DateTimeStyles) method, except that this method does not throw an exception if the conversion fails. It parses the string representation of a date and time that must exactly match the pattern specified by the format parameter. If the input string does not match this pattern, with some possible variations in white space defined by the styles parameter, the parsing operation fails and the method returns false.

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 pattern of input. 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 parsing operation fails and the method returns false.

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 offset of the resulting DateTimeOffset object depends on the value of the styles parameter. If styles includes AssumeLocal, the offset of the local time zone is assigned to the DateTimeOffset object. If styles includes AssumeUniversal, the Coordinated Universal Time (UTC) offset, or +00:00, is assigned to the DateTimeOffset object. If neither value is specified, the offset of the local time zone is used.

The particular date and time symbols and strings used in input are defined by the formatProvider parameter. The same is true for the precise pattern 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.

The styles parameter defines whether white space is allowed in the input string, indicates how strings without an explicit offset component are parsed, and supports UTC conversion as part of the parsing operation. All members of the DateTimeStyles enumeration are supported except NoCurrentDateDefault. The following table lists the effect of each supported member.

DateTimeStyles member

Behavior

AdjustToUniversal

Parses input and, if necessary, converts it to UTC. It is equivalent to parsing a string, and then calling the DateTimeOffset.ToUniversalTime method of the returned DateTimeOffset object.

AssumeLocal

If format does not require that input contain an offset value, the returned DateTimeOffset object is given the offset of the local time zone. This is the default behavior.

AssumeUniversal

If format does not require that input contain an offset value, the returned DateTimeOffset object is given the UTC offset (+00:00).

AllowInnerWhite

Allows input to include inner white space not specified by format. Extra white space can appear between date and time components and within individual components, other than the offset, and is ignored when parsing the string.

AllowLeadingWhite

Allows input to include leading spaces not specified by format. These are ignored when parsing the string.

AllowTrailingWhite

Allows input to include trailing spaces not specified by format. These are ignored when parsing the string.

AllowWhiteSpaces

Allows input to include leading, trailing, and inner spaces not specified by format. All extra white-space characters not specified in format are ignored when parsing the string.

None

Indicates that additional white space is not permitted in input. White space must appear exactly as specified in format. This is the default behavior.

RoundtripKind

Has no effect, because the DateTimeOffset structure does not include a Kind property.

Examples

The following example uses the TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset%) method with standard and custom format specifiers, the invariant culture, and various DateTimeStyles values 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 and assume time is UTC.
dateString = "06/15/2008"
format = "d"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                   DateTimeStyles.AssumeUniversal, _
                                   result) Then
   outputBlock.Text &= String.Format("'{0}' converts to {1}.", dateString, result.ToString()) & vbCrLf
Else
   outputBlock.Text &= String.Format("'{0}' is not in the correct format.", dateString) & vbCrLf
End If

' Parse date-only value with leading white space.
' Should return False because only trailing whitespace is  
' specified in method call.
dateString = " 06/15/2008"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowTrailingWhite, _
                                result) Then
   outputBlock.Text &= String.Format("'{0}' converts to {1}.", dateString, result.ToString()) & vbCrLf
Else
   outputBlock.Text &= String.Format("'{0}' is not in the correct format.", dateString) & vbCrLf
End If

' Parse date and time value, and allow all white space.
dateString = " 06/15/   2008  15:15    -05:00"
format = "MM/dd/yyyy H:mm zzz"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowWhiteSpaces, _
                                result) Then
   outputBlock.Text &= String.Format("'{0}' converts to {1}.", dateString, result.ToString()) & vbCrLf
Else
   outputBlock.Text &= String.Format("'{0}' is not in the correct format.", dateString) & vbCrLf
End If

' Parse date and time and convert to UTC.
dateString = "  06/15/2008 15:15:30 -05:00"
format = "MM/dd/yyyy H:mm:ss zzz"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowWhiteSpaces Or _
                                DateTimeStyles.AdjustToUniversal, _
                                result) Then
   outputBlock.Text &= String.Format("'{0}' converts to {1}.", dateString, result.ToString()) & vbCrLf
Else
   outputBlock.Text &= String.Format("'{0}' is not in the correct format.", dateString) & vbCrLf
End If
' The example displays the following output:
'    '06/15/2008' converts to 6/15/2008 12:00:00 AM +00:00.
'    ' 06/15/2008' is not in the correct format.
'    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
'    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.
string dateString, format;
DateTimeOffset result;
IFormatProvider provider = CultureInfo.InvariantCulture;

// Parse date-only value with invariant culture and assume time is UTC.
dateString = "06/15/2008";
format = "d";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                 DateTimeStyles.AssumeUniversal,
                                 out result))
   outputBlock.Text += String.Format("'{0}' converts to {1}.", dateString, result.ToString()) + "\n";
else
   outputBlock.Text += String.Format("'{0}' is not in the correct format.", dateString) + "\n";

// Parse date-only value with leading white space.
// Should return False because only trailing whitespace is  
// specified in method call.
dateString = " 06/15/2008";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                 DateTimeStyles.AllowTrailingWhite,
                                 out result))
   outputBlock.Text += String.Format("'{0}' converts to {1}.", dateString, result.ToString()) + "\n";
else
   outputBlock.Text += String.Format("'{0}' is not in the correct format.", dateString) + "\n";

// Parse date and time value, and allow all white space.
dateString = " 06/15/   2008  15:15    -05:00";
format = "MM/dd/yyyy H:mm zzz";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                 DateTimeStyles.AllowWhiteSpaces,
                                 out result))
   outputBlock.Text += String.Format("'{0}' converts to {1}.", dateString, result.ToString()) + "\n";
else
   outputBlock.Text += String.Format("'{0}' is not in the correct format.", dateString) + "\n";

// Parse date and time and convert to UTC.
dateString = "  06/15/2008 15:15:30 -05:00";
format = "MM/dd/yyyy H:mm:ss zzz";
if (DateTimeOffset.TryParseExact(dateString, format, provider,
                                DateTimeStyles.AllowWhiteSpaces |
                                DateTimeStyles.AdjustToUniversal,
                                out result))
   outputBlock.Text += String.Format("'{0}' converts to {1}.", dateString, result.ToString()) + "\n";
else
   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 +00:00.
//    ' 06/15/2008' is not in the correct format.
//    ' 06/15/   2008  15:15    -05:00' converts to 6/15/2008 3:15:00 PM -05:00.
//    '  06/15/2008 15:15:30 -05:00' converts to 6/15/2008 8:15:30 PM +00:00.

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.