来自json对象的Angular.js格式日期 [英] Angular.js format date from json object

查看:7

问题描述

我的 json 响应如下所示:

My json response look like this:

[{"Id":"dab4580b-e24d-49f8-9fd5-2e968b10d3b5","Title":"MVVM-Sidekick 入精","CreatedOn":"/Date(1390272893353)/","IsChecked":false},{"Id":"66a0f134-e240-4cc4-96fa-ac3807853ca7","Title":"Windows Phone 开发入精","CreatedOn":"/Date(1390018447080)/","IsChecked":false}]

CreatedOn"日期采用这种格式:'/Date(1390272893353)/'

the "CreatedOn" date is in this kind of format: '/Date(1390272893353)/'

当我将此结果绑定到 html 表格时,日期无法格式化:

when I bind this result to html table, the date cannot be formatted:

<td>{{item.CreatedOn | date: 'yyyy-MM-dd HH:mm'}}</td>

仍然给我:

/日期(1390272893353)/

/Date(1390272893353)/

我不想更改服务器端的任何代码(不要修改 json 字符串),格式化此日期的最佳方式是什么?

I don't want to change any code on the server side (don't modify the json string), what's the best way to format this date?

推荐答案

一种选择是编写另一个过滤器并将其放入链中.例如:

One option is to write another filter and put it in the chain. E.g.:

app.filter("mydate", function() {
    var re = //Date(([0-9]*))//;
    return function(x) {
        var m = x.match(re);
        if( m ) return new Date(parseInt(m[1]));
        else return null;
    };
});

基本上它使用正则表达式来解析字符串并生成一个 Date(如果格式与显示的格式不同,则必须调整正则表达式).

Basically it uses the regular expression to parse the string and make a Date (if the format is different than the one shown, you will have to tweak the regular expression).

将其用作:

<td>{{item.CreatedOn | mydate | date: 'yyyy-MM-dd HH:mm'}}</td>
查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆