繁体   English   中英

对于字符串格式dd / MM / yyyyTHH:mm,“字符串未被识别为有效的DateTime”

[英]“String was not recognized as a valid DateTime” for string format dd/MM/yyyyTHH:mm

我使用声明为@Html.TextBoxFor(model => model.StartDate, new { type = "datetime-local", @class = "form-control", @style = "height:34px;width:230px" })的Html.TextBoxFor接受输入@Html.TextBoxFor(model => model.StartDate, new { type = "datetime-local", @class = "form-control", @style = "height:34px;width:230px" })这两个都通过AJAX将它们的值传递给C#控制器函数,它执行以下操作

DateTime ResourceStart = DateTime.ParseExact(resource_start, "dd/MM/yyyyTHH:mm", CultureInfo.InvariantCulture);

DateTime ResourceDue = DateTime.ParseExact(resource_due, "dd/MM/yyyyTHH:mm", CultureInfo.InvariantCulture);

resource_startresource_due的值分别为“2016-12-02T09:30”和“2016-12-12T12:50”。

以下是程序崩溃时的异常细节。

System.FormatException was unhandled by user code
  HResult=-2146233033
  Message=String was not recognized as a valid DateTime.
  Source=mscorlib
  StackTrace:
       at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
       at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)
       at ITPortal.Areas.ITWorkstream.Controllers.WorkstreamController.CreateResource(Int32 task_id, String resource_start, String resource_due) in c:\Work\ITPortal\ITPortal-DEV\ITPortal\Areas\ITWorkstream\Controllers\WorkstreamController.cs:line 247
       at lambda_method(Closure , ControllerBase , Object[] )
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
  InnerException:

我认为我的问题在于我在ParseExact函数中为我的字符串声明的格式,有人能告诉我使用instad的格式吗?

您的格式字符串"dd/MM/yyyyTHH:mm"与您的输入格式不匹配2016-12-02T09:30 :格式为带有斜线的第一天,而输入为带有破折号的第一年。

您应该将输入更改为02/12/2016T09:30 ,或更改格式:

DateTime.ParseExact(resource_due, "yyyy-MM-ddTHH:mm", CultureInfo.InvariantCulture)

暂无
暂无

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

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