错误原因: 由于服务器返回给页面的数据是json格式,类似于"2012-11-10 12:18:00"的日期数据被转换成"/Date(1242357713797+0800)/",导致页面无法解析日期字段。
解决办法:
function formatDate(NewDtime) {
var dt = new Date(parseInt(NewDtime.slice(6, 19)));
var year = dt.getFullYear();
var month = dt.getMonth() + 1;
var date = dt.getDate();
var hour = dt.getHours();
var minute = dt.getMinutes();
var second = dt.getSeconds();
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}
加入上述代码即可,将json的无法解析的数据手动转换,具体列子以Easyui框架的datagrid控件为例(使用formatter:formatDate调用方法):
<table id="List">
</table>
$(function () {
$('#List').datagrid({
url: '/User/GetList',
method: 'post',
fitColumns: true,
sortName: 'id',
sortOrder: 'asc',
idField: true,
striped: true,
singleSelect: true,
rownumbers: true,
columns: [[
{ field: 'id', title: 'ID', width: 10, align: 'center' },
{ field: 'email', title: '注册邮箱', width: 20, align: 'center' },
{ field: 'pwd', title: '密码', width: 20, align: 'center' },
{ field: 'logintime', title: '登录时间', width: 20, align: 'center',formatter:formatDate}
]]
});
});
function formatDate(NewDtime) {
var dt = new Date(parseInt(NewDtime.slice(6, 19)));
var year = dt.getFullYear();
var month = dt.getMonth() + 1;
var date = dt.getDate();
var hour = dt.getHours();
var minute = dt.getMinutes();
var second = dt.getSeconds();
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}