DateTimeOffset.TryParse DateTimeOffset.TryParse DateTimeOffset.TryParse DateTimeOffset.TryParse Method

Definition

Converts a specified string representation of a date and time to its DateTimeOffset equivalent.

Overloads

TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(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.

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset)
TryParse(String, DateTimeOffset) TryParse(String, DateTimeOffset) TryParse(String, DateTimeOffset) TryParse(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.

TryParse(ReadOnlySpan<Char>, DateTimeOffset) TryParse(ReadOnlySpan<Char>, DateTimeOffset) TryParse(ReadOnlySpan<Char>, DateTimeOffset) TryParse(ReadOnlySpan<Char>, DateTimeOffset)

TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(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.

public:
 static bool TryParse(System::String ^ input, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParse (string input, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParse : string * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParse (input As String, formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

Parameters

input
String String String String

A string that contains a date and time to convert.

formatProvider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

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

styles
DateTimeStyles DateTimeStyles DateTimeStyles DateTimeStyles

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

result
DateTimeOffset DateTimeOffset DateTimeOffset 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 null or does not contain a valid string representation of a date and time. This parameter is passed uninitialized.

Returns

true if the input parameter is successfully converted; otherwise, false.

Exceptions

styles includes an undefined DateTimeStyles value.

-or-

NoCurrentDateDefault is not supported.

-or-

styles includes mutually exclusive DateTimeStyles values.

Examples

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.

string dateString;
DateTimeOffset parsedDate;

dateString = "05/01/2008 6:00:00";
// Assume time is local 
if (DateTimeOffset.TryParse(dateString, null as IFormatProvider, 
                            DateTimeStyles.AssumeLocal, 
                            out parsedDate))
   Console.WriteLine("'{0}' was converted to {1}.", 
                     dateString, parsedDate.ToString());
else
   Console.WriteLine("Unable to parse '{0}'.", dateString);    

// Assume time is UTC
if (DateTimeOffset.TryParse(dateString, null as IFormatProvider, 
                            DateTimeStyles.AssumeUniversal, 
                            out parsedDate))
   Console.WriteLine("'{0}' was converted to {1}.", 
                     dateString, parsedDate.ToString());
else
   Console.WriteLine("Unable to parse '{0}'.", dateString);    

// Parse and convert to UTC 
dateString = "05/01/2008 6:00:00AM +5:00";
if (DateTimeOffset.TryParse(dateString, null as IFormatProvider, 
                           DateTimeStyles.AdjustToUniversal, 
                           out parsedDate))
   Console.WriteLine("'{0}' was converted to {1}.", 
                     dateString, parsedDate.ToString());
else
   Console.WriteLine("Unable to parse '{0}'.", dateString);    
// The example displays the following output to the console:
//    '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM -07:00.
//    '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM +00:00.
//    '05/01/2008 6:00:00AM +5:00' was converted to 5/1/2008 1:00:00 AM +00:00.      
Dim dateString As String
Dim parsedDate As DateTimeOffset

dateString = "05/01/2008 6:00:00"
' Assume time is local 
If DateTimeOffset.TryParse(dateString, Nothing, _
                           DateTimeStyles.AssumeLocal, _
                           parsedDate) Then
   Console.WriteLine("'{0}' was converted to {1}.", _
                     dateString, parsedDate.ToString())
Else
   Console.WriteLine("Unable to parse '{0}'.", dateString)    
End If

' Assume time is UTC
If DateTimeOffset.TryParse(dateString, Nothing, _
                           DateTimeStyles.AssumeUniversal, _
                           parsedDate) Then
   Console.WriteLine("'{0}' was converted to {1}.", _
                     dateString, parsedDate.ToString())
Else
   Console.WriteLine("Unable to parse '{0}'.", dateString)    
End If

' Parse and convert to UTC 
dateString = "05/01/2008 6:00:00AM +5:00"
If DateTimeOffset.TryParse(dateString, Nothing, _
                           DateTimeStyles.AdjustToUniversal, _
                           parsedDate) Then
   Console.WriteLine("'{0}' was converted to {1}.", _
                     dateString, parsedDate.ToString())
Else
   Console.WriteLine("Unable to parse '{0}'.", dateString)    
End If
' The example displays the following output to the console:
'    '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM -07:00.
'    '05/01/2008 6:00:00' was converted to 5/1/2008 6:00:00 AM +00:00.
'    '05/01/2008 6:00:00AM +5:00' was converted to 5/1/2008 1:00:00 AM +00:00.      

Remarks

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"

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 null, 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.

See also

TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset) TryParse(ReadOnlySpan<Char>, IFormatProvider, DateTimeStyles, DateTimeOffset)

public:
 static bool TryParse(ReadOnlySpan<char> input, IFormatProvider ^ formatProvider, System::Globalization::DateTimeStyles styles, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParse (ReadOnlySpan<char> input, IFormatProvider formatProvider, System.Globalization.DateTimeStyles styles, out DateTimeOffset result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * System.Globalization.DateTimeStyles * DateTimeOffset -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), formatProvider As IFormatProvider, styles As DateTimeStyles, ByRef result As DateTimeOffset) As Boolean

Parameters

Returns

TryParse(String, DateTimeOffset) TryParse(String, DateTimeOffset) TryParse(String, DateTimeOffset) TryParse(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.

public:
 static bool TryParse(System::String ^ input, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParse (string input, out DateTimeOffset result);
static member TryParse : string * DateTimeOffset -> bool
Public Shared Function TryParse (input As String, ByRef result As DateTimeOffset) As Boolean

Parameters

input
String String String String

A string that contains a date and time to convert.

result
DateTimeOffset DateTimeOffset DateTimeOffset 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.

Returns

true if the input parameter is successfully converted; otherwise, false.

Examples

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

DateTimeOffset parsedDate;
string dateString;

// String with date only
dateString = "05/01/2008";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
   Console.WriteLine("{0} was converted to {1}.", 
                     dateString, parsedDate);

// String with time only
dateString = "11:36 PM";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
   Console.WriteLine("{0} was converted to {1}.", 
                     dateString, parsedDate);

// String with date and offset 
dateString = "05/01/2008 +7:00";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
   Console.WriteLine("{0} was converted to {1}.", 
                     dateString, parsedDate);

// String with day abbreviation
dateString = "Thu May 01, 2008";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
   Console.WriteLine("{0} was converted to {1}.", 
                     dateString, parsedDate);

// String with date, time with AM/PM designator, and offset
dateString = "5/1/2008 10:00 AM -07:00";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
   Console.WriteLine("{0} was converted to {1}.", 
                     dateString, parsedDate);
// if (run on 3/29/07, the example displays the following output
// to the console:
//    05/01/2008 was converted to 5/1/2008 12:00:00 AM -07:00.
//    11:36 PM was converted to 3/29/2007 11:36:00 PM -07:00.
//    05/01/2008 +7:00 was converted to 5/1/2008 12:00:00 AM +07:00.
//    Thu May 01, 2008 was converted to 5/1/2008 12:00:00 AM -07:00.
//    5/1/2008 10:00 AM -07:00 was converted to 5/1/2008 10:00:00 AM -07:00.                                 
Dim parsedDate As DateTimeOffset
Dim dateString As String

' String with date only
dateString = "05/01/2008"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
   Console.WriteLine("{0} was converted to {1}.", _
                     dateString, parsedDate)

' String with time only
dateString = "11:36 PM"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
   Console.WriteLine("{0} was converted to {1}.", _
                     dateString, parsedDate)

' String with date and offset 
dateString = "05/01/2008 +7:00"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
   Console.WriteLine("{0} was converted to {1}.", _
                     dateString, parsedDate)

' String with day abbreviation
dateString = "Thu May 01, 2008"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
   Console.WriteLine("{0} was converted to {1}.", _
                     dateString, parsedDate)

' String with date, time with AM/PM designator, and offset
dateString = "5/1/2008 10:00 AM -07:00"
If DateTimeOffset.TryParse(dateString, parsedDate) Then _
   Console.WriteLine("{0} was converted to {1}.", _
                     dateString, parsedDate)
' If run on 3/29/07, the example displays the following output
' to the console:
'    05/01/2008 was converted to 5/1/2008 12:00:00 AM -07:00.
'    11:36 PM was converted to 3/29/2007 11:36:00 PM -07:00.
'    05/01/2008 +7:00 was converted to 5/1/2008 12:00:00 AM +07:00.
'    Thu May 01, 2008 was converted to 5/1/2008 12:00:00 AM -07:00.
'    5/1/2008 10:00 AM -07:00 was converted to 5/1/2008 10:00:00 AM -07:00.                                 

Remarks

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.

See also

TryParse(ReadOnlySpan<Char>, DateTimeOffset) TryParse(ReadOnlySpan<Char>, DateTimeOffset) TryParse(ReadOnlySpan<Char>, DateTimeOffset) TryParse(ReadOnlySpan<Char>, DateTimeOffset)

public:
 static bool TryParse(ReadOnlySpan<char> input, [Runtime::InteropServices::Out] DateTimeOffset % result);
public static bool TryParse (ReadOnlySpan<char> input, out DateTimeOffset result);
static member TryParse : ReadOnlySpan<char> * DateTimeOffset -> bool
Public Shared Function TryParse (input As ReadOnlySpan(Of Char), ByRef result As DateTimeOffset) As Boolean

Parameters

Returns

Applies to