Converts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified formats, culture-specific format information, and style. The format of the string representation must match one of the specified formats exactly.
- input
- A string that contains a date and time to convert.
- formats
- An array of format specifiers that define the expected formats of input.
- formatProvider
- An object that supplies culture-specific formatting information about input.
- styles
- A bitwise combination of enumeration values that indicates the permitted format of input.
An object that is equivalent to the date and time that is contained in the input parameter, as specified by the formats, formatProvider, and styles parameters.
The DateTimeOffset.ParseExact(string, String[], IFormatProvider, System.Globalization.DateTimeStyles) method parses the string representation of a date that matches any one of the patterns assigned to the formats parameter. If the input string does not match any one of these patterns with any of the variations defined by the styles parameter, the method throws a FormatException. Aside from comparing input to multiple formatting patterns, this overload behaves identically to the DateTimeOffset.ParseExact(string, string, IFormatProvider, System.Globalization.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 the input parameter. When the method is called, input must match one of these patterns. For details about valid formatting codes, see Standard DateTime Format Strings and Custom DateTime 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 method throws a FormatException.
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 System.Globalization.DateTimeStyles.AssumeLocal, the offset of the local time zone is assigned to the DateTimeOffset object. If styles includes System.Globalization.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 and strings used in input are defined by the formatProvider parameter. The same is true for the precise format of input, if the matching element of formats is a standard format specifier string. The formatProvider parameter can be either of the following:
A System.Globalization.CultureInfo object that represents the culture based on which input is interpreted. The System.Globalization.DateTimeFormatInfo object returned by the System.Globalization.CultureInfo.DateTimeFormat property defines the symbols and formatting in input.
A System.Globalization.DateTimeFormatInfo object that defines the format of date and time data.
If formatprovider is null, the System.Globalization.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 System.Globalization.DateTimeStyles enumeration are supported except System.Globalization.DateTimeStyles.NoCurrentDateDefault. The following table lists the effect of each supported member.
System.Globalization.DateTimeStyles.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. |
System.Globalization.DateTimeStyles.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. |
System.Globalization.DateTimeStyles.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). |
System.Globalization.DateTimeStyles.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 (except the offset), and is ignored when parsing the string. |
System.Globalization.DateTimeStyles.AllowLeadingWhite |
Allows input to include leading spaces not specified by formats. These are ignored when parsing the string. |
System.Globalization.DateTimeStyles.AllowTrailingWhite |
Allows input to include trailing spaces not specified by formats. These are ignored when parsing the string. |
System.Globalization.DateTimeStyles.AllowWhiteSpaces |
Allows input to include leading, trailing, and inner spaces not specified by formats. All extra white-space characters not specified in the matched element in formats are ignored when parsing the string. |
System.Globalization.DateTimeStyles.None |
Indicates that additional white space is not permitted in input. White space must appear exactly as specified in a particular element of formats for a match to occur. This is the default behavior. |
System.Globalization.DateTimeStyles.RoundtripKind |
Has no effect because the DateTimeOffset structure does not include a Kind property. |