Angular.js format date from json object(来自json对象的Angular.js格式日期)
问题描述
我的 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>
这篇关于来自json对象的Angular.js格式日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:来自json对象的Angular.js格式日期


- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01