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格式日期


基础教程推荐
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01