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

Description

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

Syntax

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


public static DateTimeOffset ParseExact(
  string input,//from   w ww .  jav  a2 s . c  o  m
  string[] formats,
  IFormatProvider formatProvider,
  DateTimeStyles styles
)

Parameters

DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) has the following parameters.

  • input - A string that contains a date and time to convert.
  • formats - An array of format specifiers that define 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.

Returns

DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) method returns An object that is equivalent to the date and time that is contained in the input parameter, as specified by the formats, formatProvider, and styles parameters.

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 DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) method.


/*from w  ww.  jav  a  2  s .  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 = new DateTimeOffset();

        input = "2014";
        try
        {
            result = DateTimeOffset.ParseExact(input, formats, provider,
                                               DateTimeStyles.AllowWhiteSpaces);
           
        }
        catch (FormatException)
        {
            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