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

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 format, culture-specific format information, and style. The format of the string representation must match the specified format 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,//from ww  w .  jav  a 2s. c om
  string format,
  IFormatProvider provider,
  DateTimeStyles style,
  out DateTime result
)

Parameters

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

  • s - A string containing a date and time to convert.
  • format - The required format of s. See the Remarks section for more information.
  • provider - An object that supplies culture-specific formatting information about s.
  • style - A bitwise combination of one or more enumeration values that indicate the permitted format of s.
  • 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 either the s or format parameter is null, is an empty string, or does not contain a date and time that correspond to the pattern specified in format. This parameter is passed uninitialized.

Returns

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

Example

The following example demonstrates the DateTime.TryParseExact(String, String, IFormatProvider, DateTimeStyles, DateTime) method.


using System;/*  w ww.jav  a  2 s . c o  m*/
using System.Globalization;
public class MainClass{
  public static void Main(String[] argv){  

    CultureInfo enUS = new CultureInfo("en-US"); 
    string dateString;
    DateTime dateValue;
    
    // Parse date with no style flags.
    dateString = " 5/01/2009 8:30 AM";
    if (DateTime.TryParseExact(dateString, "g", enUS, 
                               DateTimeStyles.None, out dateValue))
       Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                         dateValue.Kind);
    else
       Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
    
    // Allow a leading space in the date string. 
    if (DateTime.TryParseExact(dateString, "g", enUS, 
                               DateTimeStyles.AllowLeadingWhite, out dateValue))
       Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                         dateValue.Kind);
    else
       Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
    
    // Use custom formats with M and MM.
    dateString = "5/01/2009 09:00";
    if (DateTime.TryParseExact(dateString, "M/dd/yyyy hh:mm", enUS, 
                               DateTimeStyles.None, out dateValue))
       Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                         dateValue.Kind);
    else
       Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
    
    // Allow a leading space in the date string. 
    if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm", enUS, 
                            DateTimeStyles.None, out dateValue))
       Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                         dateValue.Kind);
    else
       Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
    
    // Parse a string with time zone information.
    dateString = "05/01/2009 01:30:42 PM -05:00"; 
    if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, 
                            DateTimeStyles.None, out dateValue))
       Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                         dateValue.Kind);
    else
       Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
    
    // Allow a leading space in the date string. 
    if (DateTime.TryParseExact(dateString, "MM/dd/yyyy hh:mm:ss tt zzz", enUS, 
                            DateTimeStyles.AdjustToUniversal, out dateValue))
       Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                         dateValue.Kind);
    else
       Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
    
    // Parse a string represengting UTC.
    dateString = "2008-06-11T16:11:20.0904778Z";
    if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, 
                                   DateTimeStyles.None, out dateValue))
       Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                         dateValue.Kind);
    else
       Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
    
    if (DateTime.TryParseExact(dateString, "o", CultureInfo.InvariantCulture, 
                               DateTimeStyles.RoundtripKind, out dateValue))
       Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                         dateValue.Kind);
    else
       Console.WriteLine("'{0}' is not in an acceptable 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