在将时间格式从完整日历开始时间转换为c#时间范围时出错

Error in conversion of time format from full calendar start time to C# timespan

本文关键字:时间 转换 出错 开始时 范围 日历 格式 开始      更新时间:2023-09-26

我试图将fullcalendar返回的事件开始时间转换为在动作方法中使用c# timespan对象的模型绑定。fullcalendar为开始时间赋值的格式是"May 24, 2015 6:30 AM"。但是在ModelState中,我得到以下错误"值'May 24,2015 6:30 AM'对StartTime无效。"。在事件模式中,我必须以上述格式显示开始时间(2015年5月24日上午6:30)。但希望它模型绑定正确的时间跨度对象。有什么帮助吗?

关于场景的更多信息:

这是我在日历javascript中显示自定义日期时间格式的代码$ (" # StartTimeTxtBox").val(时刻(start) .format("微光"));显示时间为"May 24, 2015 6:30 AM"。我试图将此绑定到"公共TimeSpan StartTime {get;设置;}"在我的动作方法,它导致错误"值'May 24, 2015 6:30 AM'是无效的开始时间"。在绑定到服务器变量之前是否有任何方法进行转换?enter code here

更新#1:为了更清晰,我发布了这部分代码。

完整日历javascript:

<>之前select: function (start, end, allDay) {$('#modalTitle').html("添加一个新事件");//$ (' # modalBody ') . html("Hello world");$ (" # StartTimeTxtBox").val(时刻(start) .format("微光"));$ (" # EndTimeTxtBox").val(时刻(结束).format("微光"));$ (' # AppointmentDate ') .val(时刻(开始)。格式(YYYY-MM-DD hh: mm: ss));$ (" # PatientStatement")。val('请添加一些关于疾病的描述');$ (" # DoctorClinicID ") .val (@ViewBag.DoctorClinicID);$ (' # calendarModal ') .modal ();calendar.fullCalendar("取消选择");refreshCalendar ();}之前

StartTime文本框的标记:<input type='text' id="StartTimeTxtBox" class="form-control sharp-edge" placeholder="Start Time" name="StartTime" />

c#视图模型:

<>之前公共类任命模型{public int appointment{获取;设置;}public int DoctorClinicID {get;设置;}公共字符串医生名称{获取;设置;}公共日期时间任命日期{获取;设置;}公共时间跨度开始时间{获取;设置;}公共时间跨度结束时间{获取;设置;}public DateTime开始{得到{var ret =约会日期;ret = ret. add (StartTime);返回受潮湿腐烂;}}public DateTime结束{得到{var ret =约会日期;ret = ret. add (EndTime);返回受潮湿腐烂;}}public int PatientID {get;设置;}公共字符串PatientName{获取;设置;}公共任命状态{get;设置;}public int TokenNumber {get;设置;}公共字符串PatientStatement {get;设置;}公共字符串ClinicComments {get;设置;}}之前

操作方法:<>之前public JsonResult SaveAppointment(AppointmentModel){int任命= 0;如果(ModelState.IsValid){Appointment = new Appointment{约会日期=约会模型。AppointmentDate,StartTime = appointmentModel。开始时间,EndTime = appointmentModel。EndTime,任命状态id = 1,ClinicComments = appointmentModel。ClinicComments,DoctorClinicID = appointmentModel。DoctorClinicID,PatientID = appointmentModel。PatientID,PatientStatement = appointmentModel。PatientStatement,};appointment = repository.SaveAppointment(appointment);}返回Json(任命,JsonRequestBehavior.AllowGet);}

您需要使用LThh:mm momentjs格式而不是LLL。ll格式包括日期组件,您只需要时间组件。

。把

$("#StartTimeTxtBox").val(moment(start).format('LLL'));
$("#EndTimeTxtBox").val(moment(end).format('LLL'));
$('#AppointmentDate').val(moment(start).format('YYYY-MM-DD hh:mm:ss'));

$("#StartTimeTxtBox").val(moment(start).format('LT'));
$("#EndTimeTxtBox").val(moment(end).format('LT'));
$('#AppointmentDate').val(moment(start).format('YYYY-MM-DD'));

$("#StartTimeTxtBox").val(moment(start).format('hh:mm'));
$("#EndTimeTxtBox").val(moment(end).format('hh:mm'));
$('#AppointmentDate').val(moment(start).format('YYYY-MM-DD'));