日期格式打印不正确

时间:2011-06-16 16:07:18

标签: c# json

我将某些属性作为JSON字符串传递给客户端。但是,日期不会以正确的格式打印。它显示了一些数字,如1644506800 ..这就是我做的

        var query = (from n in CDC.NCDCPoints
                where n.EVENT_TYPE_ID == et && n.BeginDate == b && n.EndDate == e
                select new { 
                   n.EVENT_TYPE_ID,
               begindate1 = n.BeginDate,
                n.EndDate,
                n.BeginLAT,
                n.BeginLONG,
                n.EndLAT,
                n.EndLONG});


   if (query.Any())
   {
       return new JavaScriptSerializer().Serialize(query.ToList());
   }

在jquery中,

       $.ajax({
            type: "POST", url: "Data.aspx/CheckInsertRecord",
            data: "{EventType:'" + eventtype + "',BeginDate:'" + begindate + "'," +
                   "EndDate:'" + enddate+"' }",
            contentType: "application/json; charset=utf-8", dataType: "json",
            success: function (msg) {
         alert(msg.d);

           var data = $.parseJSON(msg.d);
           alert("A record of this event already exists in the database.\n" +msg.d+".");


            }
        });

那么你能告诉我如何以正确的格式获取日期吗?

2 个答案:

答案 0 :(得分:0)

您必须在打印前将日期反序列化

答案 1 :(得分:0)

JavaScriptSerializer在Unix时间序列化DateTime值,因此您必须先将该整数值反序列化为日期。您只需使用新日期()即可:

var data = $.parseJSON(msg.d);
var deserializedDate = new Date(parseInt(data.BeginDate.substr(6)));