日期时间值未在jQuery数据表中正确显示

时间:2018-11-23 13:12:05

标签: javascript c# datatables

我想在数据表中获取时间戳。如何格式化我的日期时间在jQuery数据表功能?

sql server datatime数据,如何编辑列,以便我无法在数据时间中获取正确的datetime。我已经尝试过使用类型:“ date-dd-mmm-yyyy”,但无法正常工作

 2018-11-23 07:49:35.073
     data type - date time

javascript

$(document).ready(function () {
                            $('#myTable').DataTable({
                                "ajax": {
                                    "url": "/Temperature/loaddata",
                                    "type": "GET",
                                    "datatype": "json"
                                },
                                "columns": [
                                        { "data": "id", "autoWidth": true },
                                        { "data": "updatedDate", "autoWidth": true, type: 'date-dd-mmm-yyyy', targets: 0 },
                                        { "data": "deviceid", "autoWidth": true },
                                        { "data": "devicename", "autoWidth": true },
                                        { "data": "temp", "autoWidth": true },
                                        { "data": "faht", "autoWidth": true }
                                ]
                            });
                        });

c#代码

public  ActionResult loaddata()
{ 
    using (smartpondEntities dc = new smartpondEntities())
    {
        var data = dc.Temperatures.OrderBy(a => a.id).ToList();
        return Json(new { data = data}, JsonRequestBehavior.AllowGet);
    }
}

json样本数据

[{"id":1,"updatedDate":"2018-11-23T07:49:35.073","DeviceTime":null,"deviceid":1,"devicename":"aaaa","temp":28.50,"faht":87.90},{"id":2,"updatedDate":"2018-11-23T07:49:42.1","DeviceTime":null,"deviceid":1,"devicename":"aaaa","temp":28.50,"faht":87.90}]

输出

/Date(1542939575073)/

exp op

23 Nov 2018 13:10:10 

2 个答案:

答案 0 :(得分:0)

您应该使用渲染功能来格式化/自定义数据

例如:

"columns": [
{ "data": "engine" },
{ "data": "browser" },
{
  "data": "platform",
  "render": function ( data, type, row, meta ) {
               return ConvertToDate(data);
            }
}

在ConvertToDate(data)函数中转换日期。

function ConvertToDate(data){
     return data;//Converted date
}

答案 1 :(得分:0)

我将moment.js用于日期时间(docs)。因此在您的渲染器中:

{
    title: "Date",// name 
    render: function (data, type, row) {//data
        return moment(row.updatedDate).format('DD/MM/YYYY hh:mm:ss');
    }
}