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

我试图在数据表中显示一些数据,我正在使用的表脚本是

$('#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"} ], }); 

表格收到的数据是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 } 

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

您可以使用“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.length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear(); } } 

对于可以为空的日期时间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();} } ]}; 

我使用时刻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(); } }] }); 

在处理js中的日期时,我总是使用moment.js( http://momentjs.com/ )。

返回的日期值在unix时间戳中,因此您需要转换它们。

这是一个小提琴样本: http : //jsfiddle.net/fws8u54g/

 var created = 1422010854246; moment.utc(created, "x").toISOString(); 

对于dot.net和javascript,您可以像@David Sopko一样使用

  { "data": "Date", "type": "date ", "render": function (value) { if (value === null) return ""; var pattern = /Date\(([^)]+)\)/;//date format from server side var results = pattern.exec(value); var dt = new Date(parseFloat(results[1])); return dt.getDate() + "." + (dt.getMonth() + 1) + "." + dt.getFullYear(); }, "autoWidth": true }, 
 { "render": function (data) { var d = new Date(data); return d.toLocaleString(); } 

要显示时间和日期,请添加以下代码:

 "data": 'Date' , "render": function (data) { var date = new Date(data); var month = date.getMonth() + 1; return (month.length > 1 ? month : "0" + month) + "/" + date.getDate() + "/" + date.getFullYear() + "  " +(date.getHours() < 10 ? ("0"+date.getHours()) : date.getHours())+ ":"+(date.getMinutes() < 10 ? ("0"+date.getMinutes()) : date.getMinutes()) ; //return date; }