String was not recognized as a valid DateTime.

String was not recognized as a valid DateTime

Follow below solution:

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

Syntax:


public static DateTime ParseExact(
string str,
string format,
IFormatProvider provider
)
str

=> contain date and time to convert.
format => It is specify that define the required format of str.

provider

=> It is a object that supplies culture-specific format information about str.

Exceptions

Exception Condition
ArgumentNullException str or format is null.
FormatException str or format is an empty string.
-or-
str does not contain a date and time that corresponds to the pattern specified in format.
-or-
The hour component and the AM/PM designator in str do not agree.

Demo of ParseExact method


using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string String, format;
      DateTime result;
      CultureInfo provider = CultureInfo.InvariantCulture;

      // Parse date-only value with invariant culture.
      String = "06/15/2008";
      format = "d";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", dateString, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", String);
      } 

      // Parse date-only value without leading zero in month using "d" format.
      // Should throw a FormatException because standard short date pattern of
      // invariant culture requires two-digit month.

      String = "6/15/2008";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", String, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", String);
      }

      // Parse date and time with custom specifier.

      String = "Sun 15 Jun 2008 8:30 AM -06:00";
      format = "ddd dd MMM yyyy h:mm tt zzz";
      try {
         result = DateTime.ParseExact(dateString, format, provider);
         Console.WriteLine("{0} converts to {1}.", String, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", String);
      }

      // Parse date and time with offset but without offset's minutes.
      // Should throw a FormatException because "zzz" specifier requires leading
      // zero in hours.

      String = "Sun 15 Jun 2008 8:30 AM -06";
      try {
         result = DateTime.ParseExact(String, format, provider);
         Console.WriteLine("{0} converts to {1}.", String, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", String);
      } 

      String = "15/06/2008 08:30";
      format = "g";
      provider = new CultureInfo("fr-FR");
      try {
         result = DateTime.ParseExact(String, format, provider);
         Console.WriteLine("{0} converts to {1}.", String, result.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("{0} is not in the correct format.", String);
      }
   }
}

// The example displays the following output:
//       06/15/2008 converts to 6/15/2008 12:00:00 AM.
//       6/15/2008 is not in the correct format.
//       Sun 15 Jun 2008 8:30 AM -06:00 converts to 6/15/2008 7:30:00 AM.
//       Sun 15 Jun 2008 8:30 AM -06 is not in the correct format.
//       15/06/2008 08:30 converts to 6/15/2008 8:30:00 AM.

About the Author

avatar