Επεξεργασία

Κοινή χρήση μέσω


DateTimeOffset.TryParseExact Method

Definition

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

Overloads

TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)

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

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset)

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

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)

Converts the representation of a date and time in a character span to its DateTimeOffset equivalent using the specified formats, culture-specific format information, and style. The format of the date and time representation must match one of the specified formats exactly.

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset)

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.

TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

public:
 static bool TryParseExact(System::String ^ input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (string input, string[] formats, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (string? input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : string * string[] * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As String, formats As String(), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

Parameters

input
String

A string that contains a date and time to convert.

formats
String[]

An array that defines the expected formats of input.

formatProvider
IFormatProvider

An object that supplies culture-specific formatting information about input.

styles
DateTimeStyles

A bitwise combination of enumeration values that indicates the permitted format of input. A typical value to specify is None.

result
DateTimeOffset

When the method returns, contains the DateTimeOffset equivalent to the date and time of input, if the conversion succeeded, or DateTimeOffset.MinValue, if the conversion failed. The conversion fails if the input does not contain a valid string representation of a date and time, or does not contain the date and time in the expected format defined by formats, or if formats is null. This parameter is passed uninitialized.

Returns

true if the input parameter is successfully converted; otherwise, false.

Exceptions

styles includes an undefined DateTimeStyles value.

-or-

NoCurrentDateDefault is not supported.

-or-

styles includes mutually exclusive DateTimeStyles values.

Examples

The following example defines multiple input formats for the string representation of a date and time and offset value, and then passes the string that is entered by the user to the TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset) method.

TextReader conIn = Console.In;
TextWriter conOut = Console.Out;
int tries = 0;
string input = String.Empty;

string[] formats = new string[] {"M/dd/yyyy HH:m zzz", "MM/dd/yyyy HH:m zzz",
                                 "M/d/yyyy HH:m zzz", "MM/d/yyyy HH:m zzz",
                                 "M/dd/yy HH:m zzz", "MM/dd/yy HH:m zzz",
                                 "M/d/yy HH:m zzz", "MM/d/yy HH:m zzz",
                                 "M/dd/yyyy H:m zzz", "MM/dd/yyyy H:m zzz",
                                 "M/d/yyyy H:m zzz", "MM/d/yyyy H:m zzz",
                                 "M/dd/yy H:m zzz", "MM/dd/yy H:m zzz",
                                 "M/d/yy H:m zzz", "MM/d/yy H:m zzz",
                                 "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz",
                                 "M/d/yyyy HH:mm zzz", "MM/d/yyyy HH:mm zzz",
                                 "M/dd/yy HH:mm zzz", "MM/dd/yy HH:mm zzz",
                                 "M/d/yy HH:mm zzz", "MM/d/yy HH:mm zzz",
                                 "M/dd/yyyy H:mm zzz", "MM/dd/yyyy H:mm zzz",
                                 "M/d/yyyy H:mm zzz", "MM/d/yyyy H:mm zzz",
                                 "M/dd/yy H:mm zzz", "MM/dd/yy H:mm zzz",
                                 "M/d/yy H:mm zzz", "MM/d/yy H:mm zzz"};
IFormatProvider provider = CultureInfo.InvariantCulture.DateTimeFormat;
DateTimeOffset result;

do {
   conOut.WriteLine("Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),");
   conOut.Write("Then press Enter: ");
   input = conIn.ReadLine();
   conOut.WriteLine();
   if (DateTimeOffset.TryParseExact(input, formats, provider,
                                   DateTimeStyles.AllowWhiteSpaces,
                                   out result))
   {
      break;
   }
   else
   {
      Console.WriteLine("Unable to parse {0}.", input);
      tries++;
   }
} while (tries < 3);
if (tries >= 3)
   Console.WriteLine("Exiting application without parsing {0}", input);
else
   Console.WriteLine("{0} was converted to {1}", input, result.ToString());
// Some successful sample interactions with the user might appear as follows:
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/08/2007 6:54 -6:00
//
//    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/8/2007 06:54 -06:00
//
//    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/5/07 6:54 -6:00
//
//    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00
let mutable result = None    
let mutable tries = 0
let mutable input = ""

let formats = 
    [| "M/dd/yyyy HH:m zzz"; "MM/dd/yyyy HH:m zzz"
       "M/d/yyyy HH:m zzz"; "MM/d/yyyy HH:m zzz"
       "M/dd/yy HH:m zzz"; "MM/dd/yy HH:m zzz"
       "M/d/yy HH:m zzz"; "MM/d/yy HH:m zzz"
       "M/dd/yyyy H:m zzz"; "MM/dd/yyyy H:m zzz"
       "M/d/yyyy H:m zzz"; "MM/d/yyyy H:m zzz"
       "M/dd/yy H:m zzz"; "MM/dd/yy H:m zzz"
       "M/d/yy H:m zzz"; "MM/d/yy H:m zzz"
       "M/dd/yyyy HH:mm zzz"; "MM/dd/yyyy HH:mm zzz"
       "M/d/yyyy HH:mm zzz"; "MM/d/yyyy HH:mm zzz"
       "M/dd/yy HH:mm zzz"; "MM/dd/yy HH:mm zzz"
       "M/d/yy HH:mm zzz"; "MM/d/yy HH:mm zzz"
       "M/dd/yyyy H:mm zzz"; "MM/dd/yyyy H:mm zzz"
       "M/d/yyyy H:mm zzz"; "MM/d/yyyy H:mm zzz"
       "M/dd/yy H:mm zzz"; "MM/dd/yy H:mm zzz"
       "M/d/yy H:mm zzz"; "MM/d/yy H:mm zzz" |]
let provider = CultureInfo.InvariantCulture.DateTimeFormat

while tries < 3 && result.IsNone do
    printfn "Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),"
    printf "Then press Enter: "
    input <- stdin.ReadLine()
    printfn ""
    match DateTimeOffset.TryParseExact(input, formats, provider, DateTimeStyles.AllowWhiteSpaces) with
    | true, dto ->
        result <- Some dto
    | _ ->
        printfn $"Unable to parse {input}."
    tries <- tries + 1

match result with
| Some result ->
    printfn $"{input} was converted to {result}"
| None ->
    printfn $"Exiting application without parsing {input}"

// Some successful sample interactions with the user might appear as follows:
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/08/2007 6:54 -6:00
//
//    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/8/2007 06:54 -06:00
//
//    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
//
//    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
//    Then press Enter: 12/5/07 6:54 -6:00
//
//    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00
 Dim conIn As TextReader = Console.In
 Dim conOut As TextWriter = Console.Out
 Dim tries As Integer = 0
 Dim input As String = String.Empty
 Dim formats() As String = {"M/dd/yyyy HH:m zzz", "MM/dd/yyyy HH:m zzz", _
                            "M/d/yyyy HH:m zzz", "MM/d/yyyy HH:m zzz", _
                            "M/dd/yy HH:m zzz", "MM/dd/yy HH:m zzz", _
                            "M/d/yy HH:m zzz", "MM/d/yy HH:m zzz", _                                 
                            "M/dd/yyyy H:m zzz", "MM/dd/yyyy H:m zzz", _
                            "M/d/yyyy H:m zzz", "MM/d/yyyy H:m zzz", _
                            "M/dd/yy H:m zzz", "MM/dd/yy H:m zzz", _
                            "M/d/yy H:m zzz", "MM/d/yy H:m zzz", _                               
                            "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz", _
                            "M/d/yyyy HH:mm zzz", "MM/d/yyyy HH:mm zzz", _
                            "M/dd/yy HH:mm zzz", "MM/dd/yy HH:mm zzz", _
                            "M/d/yy HH:mm zzz", "MM/d/yy HH:mm zzz", _                                 
                            "M/dd/yyyy H:mm zzz", "MM/dd/yyyy H:mm zzz", _
                            "M/d/yyyy H:mm zzz", "MM/d/yyyy H:mm zzz", _
                            "M/dd/yy H:mm zzz", "MM/dd/yy H:mm zzz", _
                            "M/d/yy H:mm zzz", "MM/d/yy H:mm zzz"}   
 Dim provider As IFormatProvider = CultureInfo.InvariantCulture.DateTimeFormat
 Dim result As DateTimeOffset

 Do 
    conOut.WriteLine("Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),")
    conOut.Write("Then press Enter: ")
    input = conIn.ReadLine()
    conOut.WriteLine() 
    If DateTimeOffset.TryParseExact(input, formats, provider, _
                                    DateTimeStyles.AllowWhiteSpaces, _
                                    result) Then
       Exit Do
    Else
       Console.WriteLine("Unable to parse {0}.", input)      
       tries += 1
    End If
 Loop While tries < 3
 If tries >= 3 Then
    Console.WriteLine("Exiting application without parsing {0}", input)
 Else
    Console.WriteLine("{0} was converted to {1}", input, result.ToString())                                                     
 End If 
 ' Some successful sample interactions with the user might appear as follows:
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/08/2007 6:54 -6:00
 '    
 '    12/08/2007 6:54 -6:00 was converted to 12/8/2007 6:54:00 AM -06:00         
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/8/2007 06:54 -06:00
 '    
 '    12/8/2007 06:54 -06:00 was converted to 12/8/2007 6:54:00 AM -06:00
 '    
 '    Enter a date, time, and offset (MM/DD/YYYY HH:MM +/-HH:MM),
 '    Then press Enter: 12/5/07 6:54 -6:00
 '    
 '    12/5/07 6:54 -6:00 was converted to 12/5/2007 6:54:00 AM -06:00

Remarks

The TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset) method parses the string representation of a date that matches any one of the patterns assigned to the formats array. If the input string does not match any one of these patterns with any variations defined by the styles parameter, the parsing operation fails and the method returns false. Aside from comparing input to multiple strings that contain format specifiers, this overload behaves identically to the DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) method.

The formats parameter is a string array whose elements contain either a single standard format specifier or one or more custom format specifiers that define the possible pattern of input. For details about valid formatting codes, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. If the matched element in formats 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 parse operation fails and the method returns false.

Important

Using the formats parameter of this overload to specify multiple formats can help reduce the frustration many users experience when they enter dates and times. In particular, the ability to define multiple input patterns enables an application to handle date and time representations that can either include or lack leading zeros in months, days, hours, minutes, and seconds. The example provides an illustration of this.

If the matched element in formats requires that input contain a date but not a time, the resulting DateTimeOffset object is assigned a time of midnight (0:00:00). If the matched element in formats requires that input contain a time but not a date, the resulting DateTimeOffset object is assigned the current date on the local system. If the matched element in formats 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 the matching element of formats is a standard format specifier string. The formatProvider parameter can be either of the following:

If formatprovider is null, the CultureInfo object that corresponds to the current culture is used.

The styles parameter defines whether white space is permitted 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 the matched element in formats 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 value.
AssumeUniversal If the matched element in formats 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 the elements in formats. Extra white space can appear between date and time components and within individual components (except for the offset) and is ignored when parsing the string.
AllowLeadingWhite Allows input to include leading spaces not specified by the elements in formats. These are ignored when parsing the string.
AllowTrailingWhite Allows input to include trailing spaces not specified by the elements in formats. These are ignored when parsing the string.
AllowWhiteSpaces Allows input to include leading, trailing, and inner spaces not specified by the elements in formats. All extra white-space characters not specified in the matched element in formats 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 a particular element in formats for a successful match to occur. This is the default behavior.
RoundtripKind Has no effect because the DateTimeOffset structure does not include a Kind property.

Notes to Callers

In the .NET Framework 4, the TryParseExact returns false if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. In the .NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.

Applies to

TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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

public:
 static bool TryParseExact(ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (ReadOnlySpan<char> input, ReadOnlySpan<char> format, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : ReadOnlySpan<char> * ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), format As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

Parameters

input
ReadOnlySpan<Char>

A span containing the characters that represent a date and time to convert.

format
ReadOnlySpan<Char>

A format specifier that defines the required format of input.

formatProvider
IFormatProvider

An object that supplies culture-specific formatting information about input.

styles
DateTimeStyles

A bitwise combination of enumeration values that indicates the permitted format of input. A typical value to specify is None.

result
DateTimeOffset

When the method returns, contains the DateTimeOffset equivalent to the date and time of input, if the conversion succeeded, or DateTimeOffset.MinValue if the conversion failed. The conversion fails if the

Returns

true if the input parameter is successfully converted; otherwise, false.

Exceptions

styles includes an undefined DateTimeStyles value. -or- NoCurrentDateDefault is not supported. -or- styles includes mutually exclusive DateTimeStyles values.

Remarks

This overload is like the DateTimeOffset.ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles) method, except that this method does not throw an exception if the conversion fails. It parses the representation of a date and time that must exactly match the pattern specified by the format parameter. If input 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 character span 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 null, 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 date and time representation, 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.

Applies to

TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

Converts the representation of a date and time in a character span to its DateTimeOffset equivalent using the specified formats, culture-specific format information, and style. The format of the date and time representation must match one of the specified formats exactly.

public:
 static bool TryParseExact(ReadOnlySpan<char> input, cli::array <System::String ^> ^ formats, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (ReadOnlySpan<char> input, string?[]? formats, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (ReadOnlySpan<char> input, string[] formats, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : ReadOnlySpan<char> * string[] * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As ReadOnlySpan(Of Char), formats As String(), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

Parameters

input
ReadOnlySpan<Char>

A span containing the characters that represent a date and time to convert.

formats
String[]

A array of standard or custom format strings that define the acceptable formats of input.

formatProvider
IFormatProvider

An object that supplies culture-specific formatting information about input.

styles
DateTimeStyles

A bitwise combination of enumeration values that indicates the permitted format of input. A typical value to specify is None.

result
DateTimeOffset

When the method returns, contains the DateTimeOffset equivalent to the date and time of input, if the conversion succeeded, or DateTimeOffset.MinValue if the conversion failed. The conversion fails if the

Returns

true if the input parameter is successfully converted; otherwise, false.

Exceptions

styles includes an undefined DateTimeStyles value. -or- NoCurrentDateDefault is not supported. -or- styles includes mutually exclusive DateTimeStyles values.

Remarks

This method parses the string representation of a date that matches any one of the patterns assigned to the formats array. If input does not match any one of these patterns with any variations defined by the styles parameter, the parsing operation fails and the method returns false. Aside from comparing input to multiple strings that contain format specifiers, this overload behaves identically to the DateTimeOffset.ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, DateTimeStyles) method.

The formats parameter is a string array whose elements contain either a single standard format specifier or one or more custom format specifiers that define the possible pattern of input. For details about valid formatting codes, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. If the matched element in formats 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 parse operation fails and the method returns false.

Important

Using the formats parameter of this overload to specify multiple formats can help reduce the frustration many users experience when they enter dates and times. In particular, the ability to define multiple input patterns enables an application to handle date and time representations that can either include or lack leading zeros in months, days, hours, minutes, and seconds. The example provides an illustration of this.

If the matched element in formats requires that input contain a date but not a time, the resulting DateTimeOffset object is assigned a time of midnight (0:00:00). If the matched element in formats requires that input contain a time but not a date, the resulting DateTimeOffset object is assigned the current date on the local system. If the matched element in formats 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 DateTimeStyles.AssumeLocal, the offset of the local time zone is assigned to the DateTimeOffset object. If styles includes DateTimeStyles.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 used in input are defined by the formatProvider parameter. The same is true for the precise pattern of input if the matching element of formats is a standard format specifier string. The formatProvider parameter can be either of the following:

If formatprovider is null, the CultureInfo object that corresponds to the current culture is used.

The styles parameter defines whether white space is permitted 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 the matched element in formats 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 value.
AssumeUniversal If the matched element in formats 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 the elements in formats. Extra white space can appear between date and time components and within individual components (except for the offset) and is ignored when parsing the string.
AllowLeadingWhite Allows input to include leading spaces not specified by the elements in formats. These are ignored when parsing the string.
AllowTrailingWhite Allows input to include trailing spaces not specified by the elements in formats. These are ignored when parsing the string.
AllowWhiteSpaces Allows input to include leading, trailing, and inner spaces not specified by the elements in formats. All extra white-space characters not specified in the matched element in formats 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 a particular element in formats for a successful match to occur. This is the default behavior.
RoundtripKind Has no effect because the DateTimeOffset structure does not include a Kind property.

Applies to

TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset)

Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs
Source:
DateTimeOffset.cs

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.

public:
 static bool TryParseExact(System::String ^ input, System::String ^ format, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParseExact (string input, string format, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
public static bool TryParseExact (string? input, string? format, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParseExact : string * string * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParseExact (input As String, format As String, formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

Parameters

input
String

A string that contains a date and time to convert.

format
String

A format specifier that defines the required format of input.

formatProvider
IFormatProvider

An object that supplies culture-specific formatting information about input.

styles
DateTimeStyles

A bitwise combination of enumeration values that indicates the permitted format of input. A typical value to specify is None.

result
DateTimeOffset

When the method returns, contains the DateTimeOffset equivalent to the date and time of input, if the conversion succeeded, or DateTimeOffset.MinValue, if the conversion failed. The conversion fails if the input parameter is null, 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.

Returns

true if the input parameter is successfully converted; otherwise, false.

Exceptions

styles includes an undefined DateTimeStyles value.

-or-

NoCurrentDateDefault is not supported.

-or-

styles includes mutually exclusive DateTimeStyles values.

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.

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))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   Console.WriteLine("'{0}' is not in the correct format.", dateString);

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

// 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))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   Console.WriteLine("'{0}' is not in the correct format.", dateString);

// 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))
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
else
   Console.WriteLine("'{0}' is not in the correct format.", dateString);
// 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.
let provider = CultureInfo.InvariantCulture

// Parse date-only value with invariant culture and assume time is UTC.
let dateString = "06/15/2008"
let format = "d"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AssumeUniversal) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// Parse date-only value with leading white space.
// Should return False because only trailing white space is
// specified in method call.
let dateString = " 06/15/2008"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AllowTrailingWhite) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// Parse date and time value, and allow all white space.
let dateString = " 06/15/   2008  15:15    -05:00"
let format = "MM/dd/yyyy H:mm zzz"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AllowWhiteSpaces) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// Parse date and time and convert to UTC.
let dateString = "  06/15/2008 15:15:30 -05:00"
let format = "MM/dd/yyyy H:mm:ss zzz"
match DateTimeOffset.TryParseExact(dateString, format, provider, DateTimeStyles.AllowWhiteSpaces ||| DateTimeStyles.AdjustToUniversal) with
| true, result ->
    printfn $"'{dateString}' converts to {result}."
| _ ->
    printfn $"'{dateString}' is not in the correct format."

// 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.
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
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
End If 

' Parse date-only value with leading white space.
' Should return False because only trailing white space is  
' specified in method call.
dateString = " 06/15/2008"
If DateTimeOffset.TryParseExact(dateString, format, provider, _
                                DateTimeStyles.AllowTrailingWhite, _
                                result) Then
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
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
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
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
   Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString())
Else
   Console.WriteLine("'{0}' is not in the correct format.", dateString)
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.

The following example uses a variety of DateTimeStyles values to parse an array of strings that are expected to conform to ISO 8601. As the output from the example shows, strings that are in the proper format fail to parse if:

Strings that do not specify a UTC offset are assumed to have the offset of the local time zone (in this case, -07:00, which reflects the offset of the Pacific Daylight Time zone) unless the DateTimeStyles.AssumeUniversal flag is supplied in the method call. In that case, they are assumed to be Universal Coordinated Time.

open System
open System.Globalization

let parseWithISO8601 dateStrings styles =
    printfn $"Parsing with {styles}:"
    for dateString in dateStrings do
        match DateTimeOffset.TryParseExact(dateString, "O", null, styles) with
        | true, date ->
            printfn $"""   {dateString,-35} --> {date.ToString "yyyy-MM-dd HH:mm:ss.FF zzz"}"""
        | _ ->
            printfn $"   Unable to convert '{dateString}'"

let dateStrings = 
    [ "2018-08-18T12:45:16.0000000Z"
      "2018/08/18T12:45:16.0000000Z"
      "2018-18-08T12:45:16.0000000Z"
      "2018-08-18T12:45:16.0000000"
      " 2018-08-18T12:45:16.0000000Z "
      "2018-08-18T12:45:16.0000000+02:00"
      "2018-08-18T12:45:16.0000000-07:00" ]

parseWithISO8601 dateStrings DateTimeStyles.None
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AllowWhiteSpaces
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AdjustToUniversal
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AssumeLocal
printfn "\n-----\n"
parseWithISO8601 dateStrings DateTimeStyles.AssumeUniversal

// The example displays the following output:
//      Parsing with None:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AllowWhiteSpaces:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//          2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AdjustToUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
//
//      -----
//
//      Parsing with AssumeLocal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AssumeUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] dateStrings = { "2018-08-18T12:45:16.0000000Z",
                               "2018/08/18T12:45:16.0000000Z",
                               "2018-18-08T12:45:16.0000000Z",
                               "2018-08-18T12:45:16.0000000",                               
                               " 2018-08-18T12:45:16.0000000Z ",
                               "2018-08-18T12:45:16.0000000+02:00",
                               "2018-08-18T12:45:16.0000000-07:00" }; 
      
      ParseWithISO8601(dateStrings, DateTimeStyles.None);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AllowWhiteSpaces);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AdjustToUniversal);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeLocal);
      Console.WriteLine("\n-----\n");
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeUniversal);   }

   private static void ParseWithISO8601(string[] dateStrings, DateTimeStyles styles)
   {   
      Console.WriteLine($"Parsing with {styles}:");
      DateTimeOffset date;
      foreach (var dateString in dateStrings)
      {
         if (DateTimeOffset.TryParseExact(dateString, "O", null, styles, out date))
         {
            Console.WriteLine($"   {dateString,-35} --> {date:yyyy-MM-dd HH:mm:ss.FF zzz}");
         }
         else
         {
            Console.WriteLine($"   Unable to convert '{dateString}'");
         }   
      } 
   }
}
// The example displays the following output:
//      Parsing with None:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AllowWhiteSpaces:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//          2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AdjustToUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
//
//      -----
//
//      Parsing with AssumeLocal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
//
//      -----
//
//      Parsing with AssumeUniversal:
//         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
//         Unable to convert '2018/08/18T12:45:16.0000000Z'
//         Unable to convert '2018-18-08T12:45:16.0000000Z'
//         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
//         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
//         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
//         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
Imports System.Globalization

Public Module Example
   Public Sub Main()
      Dim dateStrings() = { "2018-08-18T12:45:16.0000000Z",
                            "2018/08/18T12:45:16.0000000Z",
                            "2018-18-08T12:45:16.0000000Z",
                            "2018-08-18T12:45:16.0000000",                               
                            " 2018-08-18T12:45:16.0000000Z ",
                            "2018-08-18T12:45:16.0000000+02:00",
                            "2018-08-18T12:45:16.0000000-07:00" } 
      
      ParseWithISO8601(dateStrings, DateTimeStyles.None)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AllowWhiteSpaces)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AdjustToUniversal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeLocal)
      Console.WriteLine($"{vbCrLf}-----{vbCrLf}")
      ParseWithISO8601(dateStrings, DateTimeStyles.AssumeUniversal)   
   End Sub

   Private Sub ParseWithISO8601(dateStrings() As String, styles As DateTimeStyles)
      Console.WriteLine($"Parsing with {styles}:")
      Dim dat As DateTimeOffset
      For Each dateStr In dateStrings
         If DateTimeOffset.TryParseExact(dateStr, "O", Nothing, styles, dat) Then
            Console.WriteLine($"   {dateStr,-35} --> {dat:yyyy-MM-dd HH:mm:ss.FF zzz}")
         Else
            Console.WriteLine($"   Unable to convert '{dateStr}'")
         End If   
      Next 
   End Sub
End Module
' The example displays the following output:
'      Parsing with None:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
'
'      -----
'
'      Parsing with AllowWhiteSpaces:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
'          2018-08-18T12:45:16.0000000Z       --> 2018-08-18 12:45:16 +00:00
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
'
'      -----
'
'      Parsing with AdjustToUniversal:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 19:45:16 +00:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 10:45:16 +00:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 19:45:16 +00:00
'
'      -----
'
'      Parsing with AssumeLocal:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 -07:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00
'
'      -----
'
'      Parsing with AssumeUniversal:
'         2018-08-18T12:45:16.0000000Z        --> 2018-08-18 12:45:16 +00:00
'         Unable to convert '2018/08/18T12:45:16.0000000Z'
'         Unable to convert '2018-18-08T12:45:16.0000000Z'
'         2018-08-18T12:45:16.0000000         --> 2018-08-18 12:45:16 +00:00
'         Unable to convert ' 2018-08-18T12:45:16.0000000Z '
'         2018-08-18T12:45:16.0000000+02:00   --> 2018-08-18 12:45:16 +02:00
'         2018-08-18T12:45:16.0000000-07:00   --> 2018-08-18 12:45:16 -07:00

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 null, 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.

Notes to Callers

In the .NET Framework 4, the TryParseExact returns false if the string to be parsed contains an hour component and an AM/PM designator that are not in agreement. In the .NET Framework 3.5 and earlier versions, the AM/PM designator is ignored.

See also

Applies to