Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC字符串未被识别为有效的日期时间_C#_Asp.net Mvc_Datetime - Fatal编程技术网

C# ASP.NET MVC字符串未被识别为有效的日期时间

C# ASP.NET MVC字符串未被识别为有效的日期时间,c#,asp.net-mvc,datetime,C#,Asp.net Mvc,Datetime,我在网上搜索了一段时间,但我找到的每一个答案都不适合我 我正在尝试将字符串转换为datetime,但每次尝试不同的操作时,都会得到一个exeption:System.FormatException:'字符串未被识别为有效的datetime string temp = Request.QueryString["begintijd"]; DateTime test = DateTime.ParseExact(temp, "dd/MM/yyyy", CultureInfo.InvariantCultu

我在网上搜索了一段时间,但我找到的每一个答案都不适合我

我正在尝试将字符串转换为datetime,但每次尝试不同的操作时,都会得到一个exeption:System.FormatException:'字符串未被识别为有效的datetime

string temp = Request.QueryString["begintijd"];
DateTime test = DateTime.ParseExact(temp, "dd/MM/yyyy", CultureInfo.InvariantCulture);
temp
的值为:
2017年5月22日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)

MM/dd/yyyy hh:MM:ss
@gravity-由于他的时间是24小时格式,
hh
将不起作用,因为它用于12小时。在这种情况下,需要
HH
。感谢@gravity的帮助!仅仅是@ben buurstra及其作品注意到的变化:D