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

Description

DateTime ParseExact(String, String[], IFormatProvider, DateTimeStyles) converts the specified string representation of a date and time to its DateTime equivalent using the specified array of formats, culture-specific format information, and style. The format of the string representation must match at least one of the specified formats exactly or an exception is thrown.

Syntax

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


public static DateTime ParseExact(
  string s,/*from   ww  w.  ja v  a 2s  . c o m*/
  string[] formats,
  IFormatProvider provider,
  DateTimeStyles style
)

Parameters

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

  • s - A string that contains a date and time to convert.
  • formats - An array of allowable formats of s. For more information, see the Remarks section.
  • provider - An object that supplies culture-specific format information about s.
  • style - A bitwise combination of enumeration values that indicates the permitted format of s. A typical value to specify is None.

Returns

DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) method returns An object that is equivalent to the date and time contained in s, as specified by formats, provider, and style.

Example

The following example uses the DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles) method to ensure that a string in a number of possible formats can be successfully parsed .


//from   w  w  w. j a  v  a2  s .c  om
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] formats= {"M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", 
                         "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", 
                         "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", 
                         "M/d/yyyy h:mm", "M/d/yyyy h:mm", 
                         "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm"};
      string[] dateStrings = {"5/1/2014 6:32 PM", "05/01/2014 6:32:05 PM", 
                              "5/1/2014 6:32:00", "05/01/2014 06:32", 
                              "05/01/2014 06:32:00 PM", "05/01/2014 06:32:00"}; 
      DateTime dateValue;

      foreach (string dateString in dateStrings)
      {
         try {
            dateValue = DateTime.ParseExact(dateString, formats, 
                                            new CultureInfo("en-US"), 
                                            DateTimeStyles.None);
            Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
         }
         catch (FormatException) {
            Console.WriteLine("Unable to convert '{0}' to a date.", 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