Svenmarim Svenmarim - 1 year ago 96
C# Question

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

I searched on the internet for a while now, but every answer i found, didn't worked for me.

I am trying to convert a string to a datetime, but every time i try different things, i get the exeption: System.FormatException: 'String was not recognized as a valid DateTime.

string temp = Request.QueryString["begintijd"];
DateTime test = DateTime.ParseExact(temp, "dd/MM/yyyy", CultureInfo.InvariantCulture);

The value of
05/22/2017 15:00:00

I also tried "MM/dd/yyyy" but that also fails.

Answer Source

In your example, where temp = 05/22/2017 15:00:00, you need your 'format' in ParseExact to match that syntax.

So I would try this, DateTime test = DateTime.ParseExact(temp, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariateCulture);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download