如何根据当前日期过滤 JSON 数据 [英] How to filter JSON data based on current date
本文介绍了如何根据当前日期过滤 JSON 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想问一下 angularJS.
I wanna ask about angularJS.
我这里有 json
var friends = [
{
"id":1,
"Tanggal":"\/Date(1408060800000)\/",
"Nama":"Hari Departemen Agama Republik Indonesia"
},
{
"id":2,
"Tanggal":"\/Date(1388880000000)\/",
"Nama":"Hari Korps Wanita Angkatan Laut"
},
查看
<ul>
<li ng-repeat="friend in friends | filter:" >{{friend.Tanggal.substr(6,13) | date: 'dd MMMM' }}
{{friend.Nama}}
</li>
</ul>
这是我的控制器
.controller('FriendsCtrl', function($scope, Friends) {
$scope.friends = Friends.all();
})
我想过滤变量Tanggal"以将数据显示为当前日期(今天).所以只会显示今天的数据.
I want to filter variable "Tanggal" to show data as current date(today). so just data on today will show.
需要帮助.
推荐答案
您可以为此使用自定义过滤器:
You can use a custom filter for that:
app.filter('todayFilter', function() {
return function(items, field) {
var newItems = [];
var currentDate = new Date();
var tomorrow = new Date();
currentDate.setHours(0, 0, 0, 0);
tomorrow.setDate(currentDate.getDate() + 1);
angular.forEach(items, function(item) {
if (item[field] >= currentDate && item[field] <= tomorrow) {
newItems.push(item);
}
});
return newItems;
}
});
并像这样应用它:
<ul>
<li ng-repeat="friend in friends | todayFilter: 'Tanggal'" >{{friend.Tanggal.substr(6,13) | date: 'dd MMMM' }}
{{friend.Nama}}
</li>
</ul>
这样您就可以在应用的其他部分重复使用过滤器.
That way you can reuse your filter in other parts of your app.
这是一个 plunker 证明:http://plnkr.co/edit/qga1POO1nV0kaEzXUuFH?p=预览
Here is a plunker demonstrating that: http://plnkr.co/edit/qga1POO1nV0kaEzXUuFH?p=preview
希望有所帮助.
这篇关于如何根据当前日期过滤 JSON 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文