字符串格式的"String was not recognized as a valid DateTime" 日/月/年:毫米



我正在使用Html输入。声明为@Html.TextBoxFor(model => model.StartDate, new { type = "datetime-local", @class = "form-control", @style = "height:34px;width:230px" })的TextBoxFor有两个,它们都通过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"one_answers"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:WorkITPortalITPortal-DEVITPortalAreasITWorkstreamControllersWorkstreamController.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函数中为我的字符串声明的格式,有人能告诉我使用哪种格式吗?

您的格式字符串"dd/MM/yyyyTHH:mm"与您的输入格式2016-12-02T09:30不匹配:格式是日期优先,带斜杠,而输入是年优先,带破折号。

你应该改变你的输入为02/12/2016T09:30,或者改变你的格式:

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

相关内容

  • 没有找到相关文章

最新更新