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

Description

DateTimeOffset TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset) converts the specified string representation of a date and time to its DateTimeOffset equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly.

Syntax

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


public static bool TryParseExact(
  string input,/*w  ww.ja  v  a2s.c  o  m*/
  string format,
  IFormatProvider formatProvider,
  DateTimeStyles styles,
  out DateTimeOffset result
)

Parameters

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

  • input - A string that contains a date and time to convert.
  • format - A format specifier that defines the required format of input.
  • formatProvider - An object that supplies culture-specific formatting information about input.
  • styles - A bitwise combination of enumeration values that indicates the permitted format of input. A typical value to specify is None.
  • 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 in the expected format defined by format and provider. This parameter is passed uninitialized.

Returns

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

Example

The following example uses the TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTimeOffset) method with standard and custom format specifiers, the invariant culture, and various DateTimeStyles values to parse several date and time strings.


//from www .  j  a  v a  2 s.c  o m
using System;
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    string dateString, format;  
    DateTimeOffset result;
    IFormatProvider provider = CultureInfo.InvariantCulture;
    
    dateString = "06/15/2014";
    format = "d";
    if (DateTimeOffset.TryParseExact(dateString, format, provider, 
                                     DateTimeStyles.AssumeUniversal, 
                                     out result))
       Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
    else
       Console.WriteLine("'{0}' is not in the correct format.", dateString);
    
    dateString = " 06/15/2014";
    if (DateTimeOffset.TryParseExact(dateString, format, provider, 
                                     DateTimeStyles.AllowTrailingWhite, 
                                     out result))
       Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
    else
       Console.WriteLine("'{0}' is not in the correct format.", dateString);
    
    dateString = " 06/15/   2014  15:15    -05:00";
    format = "MM/dd/yyyy H:mm zzz";
    if (DateTimeOffset.TryParseExact(dateString, format, provider, 
                                     DateTimeStyles.AllowWhiteSpaces, 
                                     out result))
       Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
    else
       Console.WriteLine("'{0}' is not in the correct format.", dateString);
    
    dateString = "  06/15/2014 15:15:30 -05:00";   
    format = "MM/dd/yyyy H:mm:ss zzz";       
    if (DateTimeOffset.TryParseExact(dateString, format, provider, 
                                    DateTimeStyles.AllowWhiteSpaces | 
                                    DateTimeStyles.AdjustToUniversal, 
                                    out result))
       Console.WriteLine("'{0}' converts to {1}.", dateString, result.ToString());
    else
       Console.WriteLine("'{0}' is not in the correct format.", 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