{
"id":1,
"name":"Jack",
"date":"01-06-2017"
},
{
"id":2,
"name":"Allen",
"date":"07-08-2017"
},
{
"id":3,
"name":"Annie",
"date":"22-11-2017"
},
This JSON stored in 'members' Array I want filter the member based on start and end date like:
let startDate;
let endDate;
let selectedMembers = this.members.filter(m => m.date > startDate && m.date < endDate);
something like that..
You can use the array_filter() function to filter multidimensional array by date range using PHP. This example will show you how to filter array by specific date range in PHP. The following code snippet helps to filter last month events from an array using the array_filter() and strtotime() function in PHP.
Filter is an important part in AngularJS as well as Angular 2 or Angular 4. It is basically used to filter an item from a group of items, which are there in an array or an object array. It selects a subset of the items from an array and returns it as a new array and this item is displayed on UI.
AngularJS date filter is used to convert a date into a specified format. When the date format is not specified, the default date format is 'MMM d, yyyy'. Parameter Values: The date filter contains format and timezone parameters which is optional.
This is the solution of my problem:
members:any[] = [{
"id":1,
"name":"Jack",
"date":"01-06-2017"
},
{
"id":2,
"name":"Allen",
"date":"07-08-2017"
},
{
"id":3,
"name":"Annie",
"date":"22-11-2017"
}];
let start = "01-02-2017";
let end = "06-07-2017";
let.selectedMembers = this.members.filter(
m => new Date(m.date) >= new Date(startDate) && new Date(m.date) <= new Date(endDate)
);
console.log(selectedMembers);
u need first to transform the dates from string to Date type in order to compare them properly then do something like:
let members: any[] = [{
"id": 1,
"name": "Jack",
"date": "01-06-2017" // this should be of object Date()
},
{
"id": 2,
"name": "Allen",
"date": "07-08-2017" // this should be of object Date()
},
{
"id": 3,
"name": "Annie",
"date": "22-11-2017" // this should be of object Date()
}];
let start = "01-02-2017; // this should be of object Date()
let end = "06-07-2017"; // this should be of object Date()
let selectedMembers = members.filter(m => {
if ( m.date > start && m.date < end) // or you can cast here to Date()
return m;
});
console.log(selectedMembers);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With