Export (0) Print
Expand All

DateTimeOffset::ParseExact Method (String, String, IFormatProvider, DateTimeStyles)

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)

public:
static DateTimeOffset ParseExact(
	String^ input, 
	String^ format, 
	IFormatProvider^ formatProvider, 
	DateTimeStyles styles
)

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.
styles
Type: System.Globalization::DateTimeStyles
A bitwise combination of enumeration values that indicates the permitted format of input.

Return Value

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

ExceptionCondition
ArgumentException

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

-or-

The styles parameter includes an unsupported value.

-or-

The styles parameter contains DateTimeStyles values that cannot be used together.

ArgumentNullException

input is nullptr.

-or-

format is nullptr.

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.

-or-

The hour component and the AM/PM designator in input do not agree.

The DateTimeOffset::ParseExact(String, String, IFormatProvider, DateTimeStyles) 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 the pattern of the format parameter, with any variations defined by the styles parameter, the method throws a FormatException. In contrast, the DateTimeOffset::Parse(String, IFormatProvider, DateTimeStyles) method parses the string representation of a date in any one of the formats recognized by the format provider'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 pattern 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 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 format of input, if format is a standard format specifier string. The formatProvider parameter can be either of the following:

If formatprovider is nullptr, 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 value.

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, 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 ParseExact method throws a FormatException 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.

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

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5 SP1, 3.0 SP1, 2.0 SP1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft