Question

I am using following code:

System.DateTime.ParseExact(
    dateString,
    "MMddyyyy",
    System.Globalization.DateTimeFormatInfo.InvariantInfo);

But getting error as String was not recognized as a valid DateTime.

Can anyone let me know how to get the exact format?

Was it helpful?

Solution

So you just have the wrong format. The documentation states that to parse a string like that you'd need a format like MMMM dd, yyyy.

System.DateTime.ParseExact(dateString,
    "MMMM dd, yyyy",
    System.Globalization.DateTimeFormatInfo.InvariantInfo);

Take note in the documentation to the following:

  • MMMM - The full name of the month.
  • dd - The day of the month, from 01 through 31.
  • yyyy - The year as a four-digit number.

OTHER TIPS

Try with

string date = "January, 09 2012";
DateTime d = DateTime.ParseExact(date, "MMMM, dd yyyy", 
                                 CultureInfo.InvariantCulture, DateTimeStyles.None);
Console.WriteLine(d.ToString("MMddyyyy"));

You can try following code:

string dateInput = "January 09, 2012";

DateTime dt = DateTime.ParseExact(dateInput, "MMMM dd, yyyy", 
System.Globalization.CultureInfo.InvariantCulture, 
System.Globalization.DateTimeStyles.None);

string dateOutput = dt.ToString("MMddyyyy");

Output:

01092012

Please Try Following Code

string DateFormate = "01/22/2013 10:00:00";
DateTime dt = DateTime.ParseExact(DateFormate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); DateFormate = dt.ToString("dd/M/yyyy");

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top