DateTimeOffset.Parse Method (System) (original) (raw)

Source:

DateTimeOffset.cs

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 culture-specific format information and formatting style.

public:
 static DateTimeOffset Parse(System::String ^ input, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles);
public static DateTimeOffset Parse(string input, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles);
public static DateTimeOffset Parse(string input, IFormatProvider? formatProvider, System.Globalization.DateTimeStyles styles);
static member Parse : string * IFormatProvider * System.Globalization.DateTimeStyles -> DateTimeOffset
Public Shared Function Parse (input As String, formatProvider As IFormatProvider, styles As DateTimeStyles) As DateTimeOffset

Parameters

input

String

A string that contains a date and time to convert.

formatProvider

IFormatProvider

An object that provides culture-specific format 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.

Returns

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

Exceptions

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

-or-

styles is not a valid DateTimeStyles value.

-or-

styles includes an unsupported DateTimeStyles value.

-or-

styles includes DateTimeStyles values that cannot be used together.

input does not contain a valid string representation of a date and time.

-or-

input contains the string representation of an offset value without a date or time.

Examples

The following example illustrates the effect of passing the DateTimeStyles.AssumeLocal, DateTimeStyles.AssumeUniversal, and DateTimeStyles.AdjustToUniversal values to the styles parameter of the Parse(String, IFormatProvider, DateTimeStyles) method.

string dateString;
DateTimeOffset offsetDate;

dateString = "05/01/2008 6:00:00";
// Assume time is local
offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AssumeLocal);
Console.WriteLine(offsetDate.ToString());   // Displays 5/1/2008 6:00:00 AM -07:00

// Assume time is UTC
offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AssumeUniversal);
Console.WriteLine(offsetDate.ToString());   // Displays 5/1/2008 6:00:00 AM +00:00

// Parse and convert to UTC
dateString = "05/01/2008 6:00:00AM +5:00";
offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AdjustToUniversal);
Console.WriteLine(offsetDate.ToString());   // Displays 5/1/2008 1:00:00 AM +00:00
let dateString = "05/01/2008 6:00:00"
// Assume time is local
let offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AssumeLocal)
printfn $"{offsetDate}"   // Displays 5/1/2008 6:00:00 AM -07:00

// Assume time is UTC
let offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AssumeUniversal)
printfn $"{offsetDate}"   // Displays 5/1/2008 6:00:00 AM +00:00

// Parse and convert to UTC
let dateString = "05/01/2008 6:00:00AM +5:00"
let offsetDate = DateTimeOffset.Parse(dateString, null, DateTimeStyles.AdjustToUniversal)
printfn $"{offsetDate}"   // Displays 5/1/2008 1:00:00 AM +00:00
Dim dateString As String
Dim offsetDate As DateTimeOffset

dateString = "05/01/2008 6:00:00"
' Assume time is local 
offsetDate = DateTimeOffset.Parse(dateString, Nothing, DateTimeStyles.AssumeLocal)
Console.WriteLine(offsetDate.ToString())   ' Displays 5/1/2008 6:00:00 AM -07:00

' Assume time is UTC
offsetDate = DateTimeOffset.Parse(dateString, Nothing, DateTimeStyles.AssumeUniversal)
Console.WriteLine(offsetDate.ToString())   ' Displays 5/1/2008 6:00:00 AM +00:00

' Parse and convert to UTC 
dateString = "05/01/2008 6:00:00AM +5:00"
offsetDate = DateTimeOffset.Parse(dateString, Nothing, DateTimeStyles.AdjustToUniversal)
Console.WriteLine(offsetDate.ToString())   ' Displays 5/1/2008 1:00:00 AM +00:00

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.

Element Example
"2/10/2007"
"1:02:03 PM"
"-7:30"

Although each of these elements is optional, cannot appear by itself. It must be provided together with either or

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 and

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

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

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

DateTimeStyles member Description
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.
AllowInnerWhite Although valid, this value is ignored. Inner white space is permitted in the and
AllowLeadingWhite Although valid, this value is ignored. Leading white space is permitted in front of each component in the parsed string.
AllowTrailingWhite Although valid, this value is ignored. Trailing white space is permitted in front of each component in the parsed string.
AllowWhiteSpaces This is the default behavior. It cannot be overridden by supplying a more restrictive DateTimeStyles enumeration value, such as DateTimeStyles.None.
AssumeLocal Indicates that, if the input parameter lacks an element, the offset of the local time zone should be provided. This is the default behavior of the Parse method.
AssumeUniversal Indicates that, if the input parameter lacks an element, the UTC offset (00:00) should be provided.
None Although valid, this value is ignored and has no effect.
RoundtripKind Because the DateTimeOffset structure does not include a Kind property, this value has no effect.

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

See also

Applies to