繁体   English   中英

无法将ASP.NET MVC字符串识别为有效的DateTime

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

我现在在互联网上搜索了一段时间,但是找到的每个答案都对我不起作用。

我正在尝试将字符串转换为日期时间,但是每次尝试不同的操作时,都会得到例外:System.FormatException:'字符串未被识别为有效的DateTime。

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

temp的值是: 05/22/2017 15:00:00

我也尝试过“ MM / dd / yyyy”,但是也失败了。

在您的示例中,其中temp = 05/22/2017 15:00:00,您需要ParseExact中的“格式”以匹配该语法。

所以我会尝试一下, DateTime test = DateTime.ParseExact(temp, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariateCulture);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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