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

Last Reply 11 months ago By dharmendr

Posted 11 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”?"

Posted 11 months ago
Hi @GuruLogu,
Please try the following

Convert dd/mm/yyyy format string to DateTime in c# .Net

It might help you.

Cheers Andrea.

Posted 11 months ago

Thanks Andrea!

But I need in MVC


Posted 11 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: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html