[FormatException: String was not recognized as a valid DateTime.]

[FormatException: String was not recognized as a valid DateTime.]


I am getting the below exception in my website. I searched on the internet, but every answer I found, didn't worked for me.


I am trying to convert a string to a datetime, but I get the FormatException exception.


	Convert.ToDateTime("30/11/2020")

Server Error in '/' Application. String was not recognized as a valid DateTime.


System.Convert.ToDateTime(String value)


Not-Recognized-DateTime-1.png

Not-Recognized-DateTime-2.png

In my example, where Convert.ToDateTime("30/11/2020"), I need to format as shown below.


SOLUTION - I found one solution that solve my problem. You can try this :


public DateTime DateFormat(string updDate)
{
	string day, month, year;
   
	day = updDate.Substring(0, 2);
	month = updDate.Substring(3, 2);
	year = updDate.Substring(6, 4);
	
	DateTime dt = new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day));
	
	return dt;	
}

	DateFormat("30/11/2020")

tag : String was not recognized as a valid DateTime. ASP.NET MVC