项目中前端页面需要显示时间,通过ajax从后端获取的datatables中的时间数据是时间戳,如下所示:
deviceId:"12345"
deviceName:"12345"
from:"12346"
lat:34.27745
lng:108.99563
messageType:105
termId:13197
time:1486708318863
type:1
展示datatabs数据使用函数
function loadDataList() {
if (msgQueryTable == null) {
msgQueryTable = $('#msgQueryList').dataTable({
bAutoWidth: false,
iDisplayLength: 10,
bPaginate: true,
bInfo: true,
bFilter: false,
bSort: false,
bProcessing: true,
sServerMethod: 'POST',
bServerSide: true,
sAjaxSource: '',
"aoColumns": [
{
"mData": "msg"
}, {
"mData": "from"
}, {
"mData": "termId"
}, {
"mData": function(obj){
return Eedo.DateTime(obj.time)//通过调用函数来格式化所获取的时间戳
}
},],
],
fnServerData: function (sUrl, aoData, fnCallback, oSettings) {
var aData = addServerParams(aoData, oSettings);
oSettings.jqXHR = $.ajax({
url: 请求地址和参数
data: aData,
dataType: "json",
cache: false,
type: oSettings.sServerMethod,
success: function (json) {
if (json.sError) {
oSettings.oApi._fnLog(oSettings, 0, json.sError);
}
console.log(json);
if (json.data) {
var data = json.data
} else {
var data = json
}
$(oSettings.oInstance).trigger('xhr', [oSettings, data]);
oSettings.oApi._fnProcessingDisplay(oSettings, false); //关闭加载框
fnCallback(data);
},
error: function (xhr, error, thrown) {
if (error == "parsererror") {
oSettings.oApi._fnLog(oSettings, 0, "DataTables warning: JSON data from " +
"server could not be parsed. This is caused by a JSON formatting error.");
}
}
});
}
})
}
}
调用的Eedo方法,获得"yyyy-mm-dd hh:ii:ss"格式的时间
Eedo.DateTime=function getMyDate(time){
var oDate = new Date(time),
oYear = oDate.getFullYear(),
oMonth = oDate.getMonth()+1,
oDay = oDate.getDate(),
oHour = oDate.getHours(),
oMin = oDate.getMinutes(),
oSen = oDate.getSeconds(),
oTime = oYear +'-'+ getzf(oMonth) +'-'+ getzf(oDay) +' '+ getzf(oHour) +':'+ getzf(oMin) +':'+getzf(oSen);//最后拼接时间
return oTime;
};
//补0操作,当时间数据小于10的时候,给该数据前面加一个0
function getzf(num){
if(parseInt(num) < 10){
num = '0'+num;
}
return num;
}