C# DateTimeOffset TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset) Array

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 array of formats, culture-specific format information, and style. The format of the string representation must match one of the specified formats exactly.

Syntax

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


public static bool TryParseExact(
  string input,//from  w w  w. j  a va 2s  .c  o  m
  string[] formats,
  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.
  • formats - An array that defines the expected formats 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 does not contain a valid string representation of a date and time, or does not contain the date and time in the expected format defined by format, or if formats is null. 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 defines multiple input formats for the string representation of a date and time and offset value, and then passes the string that is entered by the user to the TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset) method.


//w  ww . j  av a  2s .c o  m
using System;
using System.Globalization;
public class MainClass
{
    public static void Main(String[] argv)
    {
        string input = String.Empty;

        string[] formats = new string[] {"M/dd/yyyy HH:m zzz", "MM/dd/yyyy HH:m zzz", 
                                     "M/d/yyyy HH:m zzz", "MM/d/yyyy HH:m zzz", 
                                     "M/dd/yy HH:m zzz", "MM/dd/yy HH:m zzz", 
                                     "M/d/yy HH:m zzz", "MM/d/yy HH:m zzz",                                 
                                     "M/dd/yyyy H:m zzz", "MM/dd/yyyy H:m zzz", 
                                     "M/d/yyyy H:m zzz", "MM/d/yyyy H:m zzz", 
                                     "M/dd/yy H:m zzz", "MM/dd/yy H:m zzz", 
                                     "M/d/yy H:m zzz", "MM/d/yy H:m zzz",                               
                                     "M/dd/yyyy HH:mm zzz", "MM/dd/yyyy HH:mm zzz", 
                                     "M/d/yyyy HH:mm zzz", "MM/d/yyyy HH:mm zzz", 
                                     "M/dd/yy HH:mm zzz", "MM/dd/yy HH:mm zzz", 
                                     "M/d/yy HH:mm zzz", "MM/d/yy HH:mm zzz",                                 
                                     "M/dd/yyyy H:mm zzz", "MM/dd/yyyy H:mm zzz", 
                                     "M/d/yyyy H:mm zzz", "MM/d/yyyy H:mm zzz", 
                                     "M/dd/yy H:mm zzz", "MM/dd/yy H:mm zzz", 
                                     "M/d/yy H:mm zzz", "MM/d/yy H:mm zzz"};
        IFormatProvider provider = CultureInfo.InvariantCulture.DateTimeFormat;
        DateTimeOffset result;

        input = "31/12/09";
        if (DateTimeOffset.TryParseExact(input, formats, provider,
                                        DateTimeStyles.AllowWhiteSpaces,
                                        out result))
        {
          
        }
        else
        {
            Console.WriteLine("Unable to parse {0}.", input);
        }
        Console.WriteLine(result.ToString());

    }
}

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