Filter Array in Array by date between 2 dates
You need to return
the compairing value, either explicit
data.series = data.series.filter((item: any) => {
return item.date.getTime() >= fromDate.getTime() &&
item.date.getTime() <= toDate.getTime();
});
or without the brackets, implicit.
data.series = data.series.filter((item: any) =>
item.date.getTime() >= fromDate.getTime() && item.date.getTime() <= toDate.getTime()
);
Javascript filtering between dates to get records in specific date range
Your problem lies within the date-format you use to do the comparison.
Lets take for example this two dates:
- "10/31/2020"
- "11/30/2019"
A human can see that the date in 2020 is not smaller than the date in 2019, but for the if-comparison this:
if ("10/31/2020" <= "11/30/2019")
is the same as writing:
if ("10312020" <= "11302019")
You can try this in your browser console by typing
console.log("10/31/2020" <= "11/30/2019");
You should probably use a unix timestamp for date-comparison or use the y/m/d Format if you want to do a comparison with strings.
How can I filter an array of objects between two dates?
You might do as follows;
var data = [{ version: "3.1.1", released_on: "2016-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 }, { version: "3.1.1", released_on: "2011-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 }, { version: "3.1.1", released_on: "2009-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 }, { version: "3.1.1", released_on: "2006-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 }, { version: "3.1.1", released_on: "2013-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 }, { version: "3.1.1", released_on: "2017-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 }, { version: "3.1.1", released_on: "2015-08-21T00:00:00.000Z", high_vulns: 15, medium_vulns: 10, low_vulns: 5 }, ], ed = new Date("2016-08-21T00:00:00.000Z").getTime(), sd = new Date("2010-08-21T00:00:00.000Z").getTime(), result = data.filter(d => {var time = new Date(d.released_on).getTime(); return (sd < time && time < ed); });console.log(result);
PHP: Return all dates between two dates in an array
You could also take a look at the DatePeriod class:
$period = new DatePeriod(
new DateTime('2010-10-01'),
new DateInterval('P1D'),
new DateTime('2010-10-05')
);
Which should get you an array with DateTime objects.
To iterate
foreach ($period as $key => $value) {
//$value->format('Y-m-d')
}
Problem filtering Array in Array by date between 2 dates
According to the comment section of problem,
filter() method doesn't modify the original array. instead, it creates a new one.
this.orders = this.orders.filter()
will be caused to lose original array. Therefore, you need to store it in different variable before. (Ex:this.originalOrders
)Then
this.originalOrders
can be used for filtering,
filterByDate(d) {
this.orders = this.originalOrders.filter(
(element) =>
element.order.orderdate >= d[0] &&
element.order.orderdate <= d[1]
);
},
Thanks!
Filtering date between two dates in javascript
The better way always to use library like date-fns or moment.js. Using vanilla approach. You can convert date in timestamp. I am assuming you are getting these date:
var startDate = "01.07.2020";
var endDate = "18.08.2020";
const detailedData = [{date:"01.07.2020"},{date:"20.07.2020"},{date:"25.07.2020"},{date:"01.08.2020"},{date:"18.08.2020"},{date:"01.09.2020"},{date:"01.10.2020"}]
const filteredDate = detailedData.filter(item => {
let itemDate = item.date.split('.').reverse().join("-")
let startDateModified = startDate.split('.').reverse().join("-")
let endDateModified = endDate.split('.').reverse().join("-")
return (new Date(itemDate).getTime() >= new Date(startDateModified).getTime() && new Date(itemDate).getTime() <= new Date(endDateModified).getTime());
})
console.log("result",filteredDate);
Filter an ArrayList to between two dates
From the stream of receipts
, you need to filter the elements based on dates (i.e. not before date1
and not after date2
), sort the resulting stream, collect it into a List
and return the same.
Steps:
- Parse the date stings using
DateTimeFormatter
. - Create a stream from
receipts
and applyfilter
with the required criteria. - Sort the resulting stream.
- Collect the resulting stream into a
List
and return the same.
Code:
public List<Receipts> sortListByDate(String date1, String date2, List<Receipts> receipts) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate localDate1 = LocalDate.parse(date1, formatter);
LocalDate localDate2 = LocalDate.parse(date2, formatter);
return receipts.stream()
.filter(e -> !LocalDate.parse(e.date(), formatter).isBefore(localDate1)
&& !LocalDate.parse(e.date(), formatter).isAfter(localDate2))
.sorted()
.collect(Collectors.toList());
}
Filter array of dates
You could just filter the values with a trimmed min
and max
value.
var array = ["08:00", "09:00", "10:10", "10:30", "10:50", "11:30", "11:50", "12:00"], min = "09:00", max = "11:30", result = array.filter(a => a > min && a < max);
console.log(result);
Related Topics
Pass Input Tag Value to as Href Parameter
Multiple Key Names, Same Pair Value
Convert Milliseconds to Hours and Minutes Using Momentjs
Save Image File from Image Url JavaScript HTML
Pass Data from HTML Table to Modal and Then to PHP Script
Angular - Displaying Object Within Object in Json
Accessing Ejs Variable in JavaScript Logic
Javascript/Regex: Remove Text Between Parentheses
How to Find Middle Element of Array in JavaScript
Convert JavaScript Date Format to Yyyy-Mm-Ddthh:Mm:Ss
Safari Browser Audio.Play() Is Not Working
How to Display an Image from an Array of Images in React
How to Compare Dates in Mongo Query
How to Separate String and Push to React State
How to Make a Bot React to Its Own Message
Break the Loop of an Array Looping Function (Map, Foreach, etc.)