Error: 'String was not recognized as a valid DateTime format dd/MM/yyyy' in ASP.Net MVC

Last Reply 4 months ago By dharmendr

Posted 4 months ago

Code Behind in Controller 

string _pckdate = Convert.ToDateTime(Request.Form["txtpickupdate"]).ToShortDateString() + " " + Convert.ToDateTime(Request.Form["txtpickuptime"]).ToShortTimeString();
adbook.pickupdate_ = DateTime.ParseExact(_pckdate, "MM/dd/yyyy HH:mm tt", System.Globalization.CultureInfo.InvariantCulture);

it will shows an error: "String was not recognized as a valid DateTime “ format dd/MM/yyyy”?"

You are viewing reply posted by: dharmendr 4 months ago.
Posted 4 months ago

Hi GuruLogu,

Refer the below code.

string dateString = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
DateTime dt = new DateTime();
string result;
try
{
    result = Convert.ToDateTime(dateString).ToString("MM/dd/yyyy HH:mm tt", CultureInfo.InvariantCulture);
    dt = Convert.ToDateTime(result);
}
catch (FormatException)
{
            
}

 

I agree, here is the link: http://e-iceblue.com/free-api