C# DateTimeOffset TryParse(String, IFormatProvider, DateTimeStyles, DateTimeOffset)

Description

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.

Syntax

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


public static bool TryParse(
  string input,/*from   w w w. j  a v a2 s.  c om*/
  IFormatProvider formatProvider,
  DateTimeStyles styles,
  out DateTimeOffset result
)

Parameters

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

  • input - A string that contains a date and time to convert.
  • formatProvider - An object that provides culture-specific formatting information about input.
  • styles - A bitwise combination of enumeration values that indicates the permitted format of input.
  • result - 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

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

Example

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.


//from  ww w.j  a va 2s.c  o m
using System;
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    string dateString;
    DateTimeOffset parsedDate;
    
    dateString = "05/01/2014 6:00:00";
    // Assume time is local  
    if (DateTimeOffset.TryParse(dateString, null as IFormatProvider, 
                                DateTimeStyles.AssumeLocal, 
                                out parsedDate))
       Console.WriteLine("'{0}' was converted to 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 to {1}.", 
                         dateString, parsedDate.ToString());
    else
       Console.WriteLine("Unable to parse '{0}'.", dateString);    
    
    // Parse and convert to UTC 
    dateString = "05/01/2014 6:00:00AM +5:00";
    if (DateTimeOffset.TryParse(dateString, null as IFormatProvider, 
                               DateTimeStyles.AdjustToUniversal, 
                               out parsedDate))
       Console.WriteLine("'{0}' was converted to to {1}.", 
                         dateString, parsedDate.ToString());
    else
       Console.WriteLine("Unable to parse '{0}'.", dateString);    

  }
}
    

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