model.StartDate, new { type = "datetime-local", @class = "form-co-6ren">
gpt4 book ai didi

c# - "String was not recognized as a valid DateTime"字符串格式 dd/MM/yyyy HH :mm

转载 作者:行者123 更新时间:2023-11-30 13:54:50 26 4
gpt4 key购买 nike

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

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 函数中为我的字符串声明的格式,谁能告诉我应该使用哪种格式?

最佳答案

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

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

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

关于c# - "String was not recognized as a valid DateTime"字符串格式 dd/MM/yyyy HH :mm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38972333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com