DateTimeOffset::TryParse Method (String^, DateTimeOffset%)

 

Tries to converts 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)

public:
static bool TryParse(
	String^ input,
	[OutAttribute] DateTimeOffset% result
)

Parameters

input
Type: System::String^

A string that contains a date and time to convert.

result
Type: System::DateTimeOffset%

When the method returns, contains the DateTimeOffset 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 null 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.

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

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 current culture'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. 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 formatting information in a DateTimeFormatInfo object initialized for the current culture. To parse a string that contains designated formatting that does not necessarily correspond to that of the current culture, use the TryParseExact method and provide a format specifier.

The following example calls the TryParse(String^, DateTimeOffset%) method to parse several strings with various date and time formats.

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: