在完整日历中将日期和时间转换为另一种格式

时间:2015-03-02 13:54:47

标签: c# jquery ajax model-view-controller fullcalendar

我正在使用c#MVC中的完整日历.a需要从数据库中读取日期。但是我无法通过使用JSON以正确模式读取它们因此我需要将日期转换为以下格式...

当前输出格式:2014-06-01 16:00

[{"id":"d719381c-b3e4-e311-afa9-eca86bf6cf86","title":"Annual Function","start":"2014-06-01 16:00","end":"2014-06-01 19:00"},{"id":"d719381c-b3e4-e311-afa9-eca86bf6cf86","title":"convocation day","start":"2014-06-24 00:49 12:49","end":"2014-06-24 "}]

必需输出格式:“/日期(1423087200000)/”

[{"id":1259,"title":"bvbvcbc","start":"/Date(1423845000000)/","end":"/Date(1423931400000)/","allDay":false,"description":"Notesbvbvcbc"},{"id":1263,"title":"om nmh sivaay ","start":"/Date(1423087200000)/","end":"/Date(1423432800000)/","allDay":false,"description":"Notesom nmh sivaay "},{"id":1265,"title":"vimal raturi","start":"/Date(1423546200000)/","end":"/Date(1423632600000)/","allDay":false,"description":"Notesvimal raturi"}

3 个答案:

答案 0 :(得分:1)

为什么你需要这种格式的日期?根据您使用的客户端库,您可以编写自己的绑定处理程序来处理日期。

看看这篇文章: http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx

我希望这会有所帮助。

答案 1 :(得分:1)

您必须使用Date()方法来获取unix时间戳。

var ob = [
    {"id":"d719381c-b3e4-e311-afa9-eca86bf6cf86","title":"Annual Function","start":"2014-06-01 16:00","end":"2014-06-01 19:00"},
    {"id":"d719381c-b3e4-e311-afa9-eca86bf6cf86","title":"convocation day","start":"2014-06-24 00:49","end":"2014-06-24 12:49"}
];

for (var i = 0; i < ob.length; i++ ){
    var unix_start = new Date(ob[i].start);
    var unix_end = new Date(ob[i].end);
    ob[i].start = '/Date(' + unix_start.getTime() /1000 + ')/';
    ob[i].end = '/Date(' + unix_end.getTime() /1000 + ')/';
}

答案 2 :(得分:0)

感谢您的回答,

我只需在ajax success属性

下的视图中编写以下代码即可找到答案
  $.each(data, function (i, item)
                        {
                            item.start = new Date(item.start);
                            item.end = new Date(item.end);
                      });