Condividi tramite


DateTimeOffset.TryParse Method (String, DateTimeOffset%)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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)

Syntax

'Declaration
Public Shared Function TryParse ( _
    input As String, _
    <OutAttribute> ByRef result As DateTimeOffset _
) As Boolean
public static bool TryParse(
    string input,
    out 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 nulla 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.

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: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 <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. This means that the input parameter must contain one of the current culture's representations of a date and time. 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.

Examples

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

Dim parsedDate As DateTimeOffset
Dim dateString As String

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

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

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

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

' 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 _
   outputBlock.Text += String.Format("{0} was converted to to {1}.", _
                     dateString, parsedDate) + vbCrLf
' If run on 3/29/07, the example displays the following output
':
'    05/01/2008 was converted to to 5/1/2008 12:00:00 AM -07:00.
'    11:36 PM was converted to to 3/29/2007 11:36:00 PM -07:00.
'    05/01/2008 +7:00 was converted to to 5/1/2008 12:00:00 AM +07:00.
'    Thu May 01, 2008 was converted to to 5/1/2008 12:00:00 AM -07:00.
'    5/1/2008 10:00 AM -07:00 was converted to to 5/1/2008 10:00:00 AM -07:00.                                 
DateTimeOffset parsedDate;
string dateString;

// String with date only
dateString = "05/01/2008";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
   outputBlock.Text += String.Format("{0} was converted to to {1}.",
                     dateString, parsedDate) + "\n";

// String with time only
dateString = "11:36 PM";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
   outputBlock.Text += String.Format("{0} was converted to to {1}.",
                     dateString, parsedDate) + "\n";

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

// String with day abbreviation
dateString = "Thu May 01, 2008";
if (DateTimeOffset.TryParse(dateString, out parsedDate))
   outputBlock.Text += String.Format("{0} was converted to to {1}.",
                     dateString, parsedDate) + "\n";

// 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))
   outputBlock.Text += String.Format("{0} was converted to to {1}.",
                     dateString, parsedDate) + "\n";
// if (run on 3/29/07, the example displays the following output
//:
//    05/01/2008 was converted to to 5/1/2008 12:00:00 AM -07:00.
//    11:36 PM was converted to to 3/29/2007 11:36:00 PM -07:00.
//    05/01/2008 +7:00 was converted to to 5/1/2008 12:00:00 AM +07:00.
//    Thu May 01, 2008 was converted to to 5/1/2008 12:00:00 AM -07:00.
//    5/1/2008 10:00 AM -07:00 was converted to to 5/1/2008 10:00:00 AM -07:00.                                 

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.