JSON格式的时间/Date(2367828670431)/格式转为正常的年-月-日 格式的代码

  错误原因: 由于服务器返回给页面的数据是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;
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值