代码之家  ›  专栏  ›  技术社区  ›  rahul kumar

意外的日期格式如下/date(1486495036000)/在表中

  •  0
  • rahul kumar  · 技术社区  · 8 年前

    我在数据库中插入时间,并从数据库中获取日期时间。插入值时,其插入方式如下“2017-04-11 02:09:00.000”,但在表中显示时,其显示方式如下/日期(1486495036000)/。

    密码

     SqlCommand cmd = new SqlCommand("insert into sms values('" + today + "','" + usecase + "','" + smsdata + "')", con);
    

    数据库示例数据

    updtd_date               usecase    id  sms
    2017-04-11 02:09:00.000  watertank  1   Tank filled
    

    表中updtd_日期的数据类型 smalldatetime

    控制器

     public ActionResult getSMS()
            {
                using (dbEntities dc = new dbEntities())
                {
                    var data = dc.sms.OrderByDescending(a => a.id).ToList();
                    return Json(new { data=data }, JsonRequestBehavior.AllowGet);
                }
            }
    

    看法

      <script>
            $(document).ready(function () {
                $('#myTable').DataTable({
                    "ajax": {
                        "url": "/WaterLevel/getSMS",
                        "type": "GET",
                        "datatype": "json"
                    },
                    "columns": [
                        { "data": "updtd_date", "autoWidth": true },
                        { "data": "usecase", "autoWidth": true },
                        { "data": "id", "autoWidth": true },
                        { "data": "sms", "autoWidth": true }
    
                    ]
                });
            });
        </script>
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Amit Kumar    8 年前

    您需要解析日期。

    如果您正在使用 moment.js 然后你可以像这样解析它。

     var date = "\/Date(1486495036000)\/";
     nowDate = new Date(parseInt(date.substr(6)));
     var result = nowDate.format("ddd mmm dd yyyy HH:MM:ss");
    

    使用 Render function 设置列值格式。