场景: 我有一个格式为“yyyy-mm-dd HH:mm:ss”的输入字符串。将其转换为日期时间格式时遇到问题。我需要将此日期时间输出到数据库列并返回与我正在传递的日期时间相对应的注释。这是我的代码:我尝试使用 Datetime.ParseExact 转换字符串,但它返回另一种格式。
我不知道哪里出错了。请帮忙
这是我的代码:[HttpPost]
public string getdailynote(string selectedDate)
{
tablenote Dnote= new tablenote;
DateTime selectedDate1 = DateTime.ParseExact(selectedDate,
"yyyy-mm-dd HH:mm:ss",
CultureInfo.InvariantCulture);
Dnote.RealDate = selectedDate1;
string daily;
daily = _scheduler.GetDailyNote(selectedDate1);
return daily;
}
视图.cshtml:
function getdailynotes() {
debugger;
var calendar = $("#SchedulerCalendar").data("kendoCalendar");
var view = calendar.value();
var kendodate = dateFormat(view, 'yyyy-mm-dd HH:mm:ss');
$.ajax({
type: "POST",
url: window.location.pathname + "Scheduler/getdailynote",
data: { selectedDate: kendodate }
})
.success(function (result) {
if (result != null) {
document.getElementById('dailynotes').value = result;
}
});
}
更新: Service.cs _schedulerfile:
public string GetDailyNote(DateTime selectedDate)
{
string returndaynote;
returndaynote = dbContext.GetDailyNote(selectedDate).SingleOrDefault();
return returndaynote;
}
from autogenerated context.cs
public virtual ObjectResult<string> GetDailyNote(Nullable<System.DateTime> realDate)
{
var realDateParameter = realDate.HasValue ?
new ObjectParameter("RealDate", realDate) :
new ObjectParameter("RealDate", typeof(System.DateTime));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<string>("GetDailyNote", realDateParameter);
}