Export (0) Print
Expand All

DateTimeOffset.TryParse Method (String, IFormatProvider, DateTimeStyles, DateTimeOffset)

Tries to convert a specified string representation of a date and time to its DateTimeOffset equivalent, and returns a value that indicates whether the conversion succeeded.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

static member TryParse : 
        input:string * 
        formatProvider:IFormatProvider * 
        styles:DateTimeStyles * 
        result:DateTimeOffset byref -> bool

Parameters

input
Type: System.String

A string that contains a date and time to convert.

formatProvider
Type: System.IFormatProvider

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

styles
Type: System.Globalization.DateTimeStyles

A bitwise combination of enumeration values that indicates the permitted format of input.

result
Type: System.DateTimeOffset

When the method returns, contains the DateTimeOffset value equivalent to the date and time of input, if the conversion succeeded, or MinValue, if the conversion failed. The conversion fails if the input parameter is a null reference (Nothing in Visual Basic) or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if the input parameter is successfully converted; otherwise, false.

ExceptionCondition
ArgumentException

styles includes an undefined DateTimeStyles value.

-or-

DateTimeStyles.NoCurrentDateDefault is not supported.

-or-

styles includes mutually exclusive DateTimeStyles values.

This overload of the TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) method is like the DateTimeOffset.Parse(String, IFormatProvider, DateTimeStyles) method, except that it does not throw an exception if the conversion fails. The 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

<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 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 Zero if either the DateTimeStyles.AdjustToUniversal or DateTimeStyles.AssumeUniversal value is specified in styles. 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 or the method returns false.

The input string is parsed by using the culture-specific formatting information in a DateTimeFormatInfo object supplied by the formatProvider parameter. The formatProvider parameter can be either of the following:

In addition, each element can be delimited by leading or trailing white space, and the <Date> and <Time> components can include inner white space (such as 6: 00:00). Only the <Offset> component cannot include inner white space.

If provider is a null reference (Nothing in Visual Basic), the 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 PositiveSign or NegativeSign properties of the NumberFormatInfo object returned by the formatprovider parameter's NumberFormat property.

The following members of the DateTimeStyles enumeration are supported:

DateTimeStyles Member

Comments

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 returned object's ToUniversalTime() method.

AllowInnerWhite

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

AllowLeadingWhite

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

AllowTrailingWhite

Although valid, this value is ignored. Trailing white space is allowed 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 <Offset> element, the offset of the local time zone should be provided. This is the default behavior of the TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) method.

AssumeUniversal

Indicates that, if the input parameter lacks an <Offset> 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.

The following example calls the TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) method with a variety of DateTimeStyles values to parse some strings with various date and time formats.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 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

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft