简体   繁体   中英

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 temp is: 05/22/2017 15:00:00

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM