C# DateTimeOffset TryParse(String, DateTimeOffset)

Description

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.

Syntax

DateTimeOffset.TryParse(String, DateTimeOffset) has the following syntax.


public static bool TryParse(
  string input,
  out DateTimeOffset result
)

Parameters

DateTimeOffset.TryParse(String, DateTimeOffset) has the following parameters.

  • input - A string that contains a date and time to convert.
  • result - 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

DateTimeOffset.TryParse(String, DateTimeOffset) method returns true if the input parameter is successfully converted; otherwise, false.

Example

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


/*from w ww. j av  a2  s . co  m*/
using System;
public class MainClass{
  public static void Main(String[] argv){  
    DateTimeOffset parsedDate;
    string dateString;
    
    // String with date only
    dateString = "05/01/2014";
    if (DateTimeOffset.TryParse(dateString, out parsedDate))
       Console.WriteLine("{0} was converted to to {1}.", 
                         dateString, parsedDate);
    
    // String with time only
    dateString = "11:36 PM";
    if (DateTimeOffset.TryParse(dateString, out parsedDate))
       Console.WriteLine("{0} was converted to to {1}.", 
                         dateString, parsedDate);
    
    // String with date and offset 
    dateString = "05/01/2014 +7:00";
    if (DateTimeOffset.TryParse(dateString, out parsedDate))
       Console.WriteLine("{0} was converted to to {1}.", 
                         dateString, parsedDate);
    
    // String with day abbreviation
    dateString = "Thu May 01, 2014";
    if (DateTimeOffset.TryParse(dateString, out parsedDate))
       Console.WriteLine("{0} was converted to to {1}.", 
                         dateString, parsedDate);
    
    // String with date, time with AM/PM designator, and offset
    dateString = "5/1/2014 10:00 AM -07:00";
    if (DateTimeOffset.TryParse(dateString, out parsedDate))
       Console.WriteLine("{0} was converted to to {1}.", 
                         dateString, parsedDate);

  }
}
    

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version