1

场景:我知道这是一个简单的日期时间转换,但我是后端和转换的新手。所以请帮助:我使用 mvc 和 ajax 将日期从 kendocalender 传递到调度程序以将其保存在数据库中:这是我的代码:controller.cs

public int insertnote(string model,DateTime kendodate)
         {
             tblDailyNotes Dnote = new tblDailyNotes();
             int noteid = 0;
             //note.RealDate=Convert.ToString(
            Dnote.Note = model;
            Dnote.RealDate = kendodate;
            noteid = _scheduler.InsertDailynote(Dnote);
             return noteid;
         }

索引.cshtml

  $("#dailynotes").change(function () {
            debugger;
            alert("Changed");
            var dailynotes = $('#dailynotes').val();

        var calendar = $("#SchedulerCalendar2").data("kendoCalendar");

        var cal = calendar.value(new Date());
        alert(cal);
        $.ajax({
           ![enter image description here][1]
            type: "POST",
            url: window.location.pathname + "Scheduler/insertnote",

            data: {model:dailynotes, kendodate: cal }
        })
         .success(function (result) {
             alert("note saved successfully");
         });
    });

问题:

这是 cal 警报

在此处输入图像描述

如何将此输出从 kendocalendar 转换为“2013-09-01 13:27:14.480”(yyyy-mm-dd hh-mm-ss.sss)这种格式,使用 jquery 通过控制器功能将其保存在数据库中。请帮忙。

4

2 回答 2

1

您可以通过 DateTime.Parse 将字符串转换为 DateTime

看看这个

于 2013-09-02T09:26:42.233 回答
-1

我同意 user2675751 的解决方案。但是,我建议使用 Datetime.TryParse() 因为这不会引发异常并返回一个布尔值来指示成功或失败。这一点在 user2675751 提供的链接中有所介绍。通常最好不要信任来自客户端的数据。

于 2013-09-02T10:13:59.720 回答