在jquery数据表中显示之前,将json日期格式化为mm/dd/yy格式

Geo*_*mas 16 jquery json date datatables

我想在显示一些数据的数据表,我使用的表脚本

$('#userData').dataTable({

        "ajax": {
                "url": "${rc.getContextPath()}/module/roles/users-list",
                "dataSrc":  "",
                },

        "columns":[
        {"data": "userId"},
        {"data": "applicationId"},
        {"data": "username"},
        {"data": "firstName"},
        {"data": "userCreated"},
        {"data": "createdTime"},
        {"data": "updatedTime"}
        ],

     });
Run Code Online (Sandbox Code Playgroud)

表格收到的数据是json,就像是

[
 {  
      "userId":179,
      "applicationId":"pgm-apn",
      "username":"collaborator.user3",
      "password":"password1",
      "email":"user@xample.com",
      "firstName":"Anthony",
      "lastName":"Gonsalves",
      "enabled":true,
      "userCreated":"sitepmadm",
      "userModified":"sitepmadm",
      "createdTime":1422454697373,
      "updatedTime":1422454697373
   },
   {  
      "userId":173,
      "applicationId":"pgm-apn",
      "username":"consumer.user",
      "password":"password1",
      "email":"test@egc.com",
      "firstName":"sherlock ",
      "lastName":"homes",
      "enabled":true,
      "userCreated":"sitepmadm",
      "userModified":"sitepmadm",
      "createdTime":1422010854246,
      "updatedTime":1422010854246
   }
Run Code Online (Sandbox Code Playgroud)

我想将日期显示为正确的日期时间.目前它在json数据中显示为相同的sting.有没有办法在数据表中转换它

nhk*_*anh 42

您可以使用"render"属性来格式化列显示http://datatables.net/reference/option/columns.render#function.

例如:

{
    "data": "createdTime",
    "render": function (data) {
        var date = new Date(data);
        var month = date.getMonth() + 1;
        return (month.toString().length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear();
    }
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*pko 19

对于可以为空的日期时间DateTime ?,您将需要使用不同的渲染函数:

        $('#userData').DataTable({
        columns: [
            { "data": "userId"},
            {"data": "userCreated",
             "type": "date ",
             "render":function (value) {
                 if (value === null) return "";

                  var pattern = /Date\(([^)]+)\)/;
                  var results = pattern.exec(value);
                  var dt = new Date(parseFloat(results[1]));

                  return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();}
            }
        ]};
Run Code Online (Sandbox Code Playgroud)


小智 6

我使用时刻js创建了demo 并使用render函数将json数据转换为所需的格式.

jsfiddle演示

也找到下面的代码:

testdata = [{
    "id": "58",
        "country_code": "UK",
        "title": "Legal Director",
        "pubdate": "1422454697373",
        "url": "http://..."
}, {
    "id": "59",
        "country_code": "UK",
        "title": "Solutions Architect,",
        "pubdate": "1422454697373",
        "url": "http://..."
}];

$('#test').dataTable({
    "aaData": testdata,
        "aoColumns": [{
        "mDataProp": "id"
    }, {
        "mDataProp": "country_code"
    }, {
        "mDataProp": "title"
    }, {
        "mDataProp": "pubdate"
    }, {
        "mDataProp": "url"
    }],
        "columnDefs": [{
        "targets": 3,
            "data": "pubdate",
            "render": function (data, type, full, meta) {
                console.log('hi...');
            console.log(data);
                console.log(type);
                console.log(full);
                console.log(meta);
            return moment.utc(data, "x").toISOString();
        }
    }]
});
Run Code Online (Sandbox Code Playgroud)