如何基于当前日期来过滤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 = preVIEW
Here is a plunker demonstrating that: http://plnkr.co/edit/qga1POO1nV0kaEzXUuFH?p=preview
希望有所帮助。
这篇关于如何基于当前日期来过滤JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文