System.DateTimeOffset.Parse Method

Converts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified culture-specific format information and formatting style.

Syntax

public static DateTimeOffset Parse (string input, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles)

Parameters

input
A string that contains a date and time to convert.
formatProvider
An object that provides culture-specific format information about input.
styles
A bitwise combination of enumeration values that indicates the permitted format of input. A typical value to specify is System.Globalization.DateTimeStyles.None.

Returns

An object that is equivalent to the date and time that is contained in input as specified by formatProvider and styles.

Remarks

This method parses a string with three elements that can appear in any order and are delimited by white space. These three elements are shown in the following table.

<Date>

"2/10/2007"

<Time>

"1:02:03 PM"

<Offset>

"-7:30:15"

Although each of these elements is optional, <Offset> cannot appear by itself. It must be provided together with either <Date> or <Time>. If <Date> is missing, its default value is the current day. If <Date> is present but its year component consists of only two digits, it is converted to a year in the provider parameter's current calendar based on the value of the System.Globalization.Calendar.TwoDigitYearMax property. If <Time> is missing, its default value is 12:00:00 AM. If <Offset> is missing, its default value is the offset of the local time zone, or TimeSpan.Zero if either the System.Globalization.DateTimeStyles.AdjustToUniversal or System.Globalization.DateTimeStyles.AssumeUniversal value is specified in the styles parameter. If <Offset> is present, it can represent either a negative or a positive offset from Coordinated Universal Time (UTC). In either case, <Offset> must include a sign symbol.

The format of these three elements is defined by the formatProvider parameter, which can be either of the following:

Each element can also be enclosed by leading or trailing white space, and the <Date> and <Time> elements can include inner white space (such as 6: 00:00). Only the <Offset> component cannot include inner white space.

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

The positive or negative sign used in <Offset> must be either + or -. It is not defined by the System.Globalization.NumberFormatInfo.PositiveSign or System.Globalization.NumberFormatInfo.NegativeSign properties of the System.Globalization.NumberFormatInfo object returned by the formatProvider parameter.

The following table shows the members of the System.Globalization.DateTimeStyles enumeration that are supported.

System.Globalization.DateTimeStyles.AdjustToUniversal

Parses the string represented by 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.AllowInnerWhite

Although valid, this value is ignored. Inner white space is permitted in the <Date> and <Time> elements.

System.Globalization.DateTimeStyles.AllowLeadingWhite

Although valid, this value is ignored. Leading white space is permitted in front of each component in the parsed string.

System.Globalization.DateTimeStyles.AllowTrailingWhite

Although valid, this value is ignored. Trailing white space is permitted in front of each component in the parsed string.

System.Globalization.DateTimeStyles.AllowWhiteSpaces

This is the default behavior. It cannot be overridden by supplying a more restrictive System.Globalization.DateTimeStyles enumeration value, such as System.Globalization.DateTimeStyles.None.

System.Globalization.DateTimeStyles.AssumeLocal

Indicates that, if the input parameter lacks an <Offset> element, the offset of the local time zone should be provided. This is the default behavior of the DateTimeOffset.Parse(string) method.

System.Globalization.DateTimeStyles.AssumeUniversal

Indicates that, if the input parameter lacks an <Offset> element, the UTC offset (00:00) should be provided.

System.Globalization.DateTimeStyles.None

Although valid, this value is ignored and has no effect.

System.Globalization.DateTimeStyles.RoundtripKind

Because the DateTimeOffset structure does not include a Kind property, this value has no effect.

Only the System.Globalization.DateTimeStyles.NoCurrentDateDefault value is not supported. An ArgumentException is thrown if this value is included in the styles parameter.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0