Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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# DateTime.ParseExact:未将字符串识别为有效的DateTime_C#_Asp.net Mvc_Datetime - Fatal编程技术网

C# DateTime.ParseExact:未将字符串识别为有效的DateTime

C# DateTime.ParseExact:未将字符串识别为有效的DateTime,c#,asp.net-mvc,datetime,C#,Asp.net Mvc,Datetime,我试图从url中获取日期和日期时间,然后将其转换为正确的格式,然后再将其存储在数据库中 var reqDate = Request.QueryString["StartDate"]; //at this point I have reqDate: 05/15/2018 00:00:00 reqDate = reqDate.Substring(0, reqDate.IndexOf(" ") + 1); //after stripping off the time part I have: 05

我试图从url中获取日期和日期时间,然后将其转换为正确的格式,然后再将其存储在数据库中

var reqDate = Request.QueryString["StartDate"];

//at this point I have reqDate: 05/15/2018 00:00:00
reqDate = reqDate.Substring(0, reqDate.IndexOf(" ") + 1);

//after stripping off the time part I have: 05/15/2018 
timingRequest.ReqDate = DateTime.ParseExact(reqDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);

//but this throws the exception
网址:

startDateTime也是如此


在第一个场景中,无需在读取indexOf后添加+1+1添加到当前为止的额外空间

//Lets take date in string is "05/15/2018 00:00:00"
Console.WriteLine(s.Substring(0, reqDate.IndexOf(" ")+1)); /*This will print "05/15/2018 " WITH EXTRA SPACE*/
正确的方法是s.Substring0,s.IndexOf

在第二个场景中,使用诸如HH:mm:ss之类的日期格式,而不是HH:mm-tt

优雅的做法是:

@斯蒂芬·穆克

查看URL后,您可以编写一个具有如下参数的方法:

public ActionResult Create(int empId, int attID, DateTime startDate, DateTime startDateTime)
{
 /*Do your work here, DefaultModelBinder will take care of parameters*/
}
第一个修复示例:

var reqDate = Request.QueryString["StartDate"];
reqDate = reqDate.Substring(0, reqDate.IndexOf(" "));

timingRequest.ReqDate = DateTime.ParseExact(reqDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
第二个修复示例:

var reqDateTime = Request.QueryString["startDateTime"];
 timingRequest.IgnoreEntry = DateTime.ParseExact(reqDateTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

与其使用asp经典样式的查询字符串解析,为什么不将DateTime StartDate作为参数添加到MVC控制器方法中呢?这样,默认的模型绑定器将为您完成所有艰苦的工作?第一个场景reqDate=reqDate.Substring0,reqDate.IndexOf@虽然我很感激你,但你不需要我的许可就可以写一个答案。继续。@Prasadelkikikar,如果您要添加答案,请告诉OP停止胡说八道,并将参数DateTime startDate、DateTime startDateTime添加到方法中,让DefaultModelBinder完成它的工作—只需将这些参数添加到您的方法-public ActionResult CreateDateTime startDate,DateTime startDateTime,您还可以为其他值(int-empID和int-attID)添加参数,DefaultModelBinder将绑定它们
var reqDate = Request.QueryString["StartDate"];
reqDate = reqDate.Substring(0, reqDate.IndexOf(" "));

timingRequest.ReqDate = DateTime.ParseExact(reqDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
var reqDateTime = Request.QueryString["startDateTime"];
 timingRequest.IgnoreEntry = DateTime.ParseExact(reqDateTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);