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

Description

DateTime TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) 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. The method returns a value that indicates whether the conversion succeeded.

Syntax

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


public static bool TryParseExact(
  string s,/*  w  ww. j  ava 2 s  .co m*/
  string[] formats,
  IFormatProvider provider,
  DateTimeStyles style,
  out DateTime result
)

Parameters

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

  • s - A string that contains a date and time to convert.
  • formats - An array of allowable formats of s. See the Remarks section for more information.
  • 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 DateTimeStyles.None.
  • result - When this method returns, contains the DateTime value equivalent to the date and time contained in s, if the conversion succeeded, or MinValue if the conversion failed. The conversion fails if s or formats is null, s or an element of formats is an empty string, or the format of s is not exactly as specified by at least one of the format patterns in formats. This parameter is passed uninitialized.

Returns

DateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime) method returns true if the s parameter was converted successfully; otherwise, false.

Example

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


using System;/*  w  w w .j a  v a2 s  .  co  m*/
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  
    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)
    {
       if (DateTime.TryParseExact(dateString, formats, 
                                  new CultureInfo("en-US"), 
                                  DateTimeStyles.None, 
                                  out dateValue))
          System.Console.WriteLine("Converted '{0}' to {1}.", dateString, dateValue);
       else
          System.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