Filter array of dates between two dates

javascript filter array of objects by date
get all months between two dates in javascript
get dates between two dates
date range filter in angular 6
find date between two dates in javascript
get all dates between two dates javascript
javascript date range
sort array by date javascript

I have an array of dates. I am trying to remove dates that are not between two dates. But I get SOME dates that are even before the "from" date. What am I doing wrong?

$scope.filterUpcomingEvents = function() {

    $scope.arrayOfEventData = JSON.parse(JSON.stringify($scope.tempArray));

    console.log($scope.tempArray, "arrayofeve");
    for (let d = 0; d < $scope.arrayOfEventData.length; d++) {
        debugger;
        console.log(d);
        console.log(new Date($scope.tempArray[d].event_date), "from", $scope.upcomingEventFrom);
        console.log(new Date($scope.tempArray[d].event_date) < $scope.upcomingEventFrom);
        console.log(new Date($scope.tempArray[d].event_date), "to", $scope.upcomingEventTo);
        console.log(new Date($scope.tempArray[d].event_date) > $scope.upcomingEventTo);
        console.log("---");
        if (new Date($scope.arrayOfEventData[d].event_date) < $scope.upcomingEventFrom || new Date($scope.arrayOfEventData[d].event_date) > $scope.upcomingEventTo) {
            $scope.arrayOfEventData.splice(d, 1);
        }
    }
    console.log($scope.arrayOfEventData, "after splice");
}

In the console, though I get true for some comparisons, it is not spliced. When I see the finaly array, it still has the value that's supposed to be spliced though it's true and enters if.

Array of dates:

0: {
        event_id: "49",
        event_name: "julyevent",
        event_date: "2018-07-13",
        event_time: "11:00AM",
        event_type_id: "2",
        …
    }
1: {
        event_id: "50",
        event_name: "up event1",
        event_date: "2018-06-22",
        event_time: "11:11AM",
        event_type_id: "2",
        …
    }
2: {
        event_id: "52",
        event_name: "twe",
        event_date: "2018-06-29",
        event_time: "11:11AM",
        event_type_id: "2",
        …
    }
3: {
        event_id: "55",
        event_name: "eve4",
        event_date: "2018-06-23",
        event_time: "10:22PM",
        event_type_id: "2",
        …
    }
4: {
        event_id: "56",
        event_name: "eve5",
        event_date: "2018-06-25",
        event_time: "11:11AM",
        event_type_id: "2",
        …
    }
5: {
        event_id: "57",
        event_name: "eve6",
        event_date: "2018-06-28",
        event_time: "10:00AM",
        event_type_id: "2",
        …
    }
6: {
        event_id: "58",
        event_name: "eve7",
        event_date: "2018-06-28",
        event_time: "9:00AM",
        event_type_id: "2",
        …
    }
7: {
        event_id: "59",
        event_name: "eve8",
        event_date: "2018-07-12",
        event_time: "7:00AM",
        event_type_id: "2",
        …
    }
8: {
        event_id: "60",
        event_name: "eve9",
        event_date: "2018-06-19",
        event_time: "4:00AM",
        event_type_id: "2",
        …
    }
9: {
        event_id: "61",
        event_name: "eve10",
        event_date: "2018-06-20",
        event_time: "8:00PM",
        event_type_id: "2",
        …
    }
10: {
        event_id: "62",
        event_name: "eve11",
        event_date: "2018-06-23",
        event_time: "3:00AM",
        event_type_id: "2",
        …
    }
11: {
        event_id: "63",
        event_name: "eve12",
        event_date: "2018-06-21",
        event_time: "10:22PM",
        event_type_id: "2",
        …
    }
12: {
        event_id: "64",
        event_name: "eve13",
        event_date: "2018-06-24",
        event_time: "6:00AM",
        event_type_id: "2",
        …
    }

When you are splicing from the same array that loop incrementally then array modifies and items get shifted. Because of that, you might miss an iteration.

One thing you can do is looping backward using a while loop so that you are not looping the direction where u splice.

let i = $scope.arrayOfEventData.length;
while(i--){
  // your code
}

Array - Filter in between two dates, I think you might want to use the filter function. The filter() method creates a new array with all elements that pass the test implemented by the provided function. To filter data to include records between two dates, you can use the FILTER function with boolean logic. In the example shown, the formula in F8 is: Which returns records with dates between January 15 and March 15, inclusive.

You can directly use Array.filter:

 $scope.arrayOfEventData =  $scope.arrayOfEventData.filter((a)=>(new Date(a.event_date) >= $scope.upcomingEventFrom || new Date(a.event_date) <= $scope.upcomingEventTo )));

Angular 2 : How to filter records between two dates?, This JSON stored in 'members' Array I want filter the member based on start and end date like: let startDate; let endDate; let  Filter array of dates between two dates. Posted on June 18, 2018 by Nobody. I have an array of dates. I am trying to remove dates that are not between two dates. But

The cause of the problem is, that when you remove some element, you still increment d by using d++. If you want to keep you code, remove d++ from the loop:

for (let d=0; d<...;      ) // no d++
...
  if ()
    splice(d, 0);
  else
    d++;

But if you use Array.filter, you avoid such problems.

Get dates in between two dates with JavaScript. · GitHub, getdates.js. // Returns an array of dates between the two dates. var getDates var dates = getDates(new Date(2013,10,22), new Date(2013,11,25));. dates. The next step is to copy the records from the table in columns A through C where the sale date is equal to or between the two selected dates. Begin by setting the dates as follows: From Date (cell G4) = 1/23/2019 To Date (cell H4) = 1/28/2019

Filtering JSON array by date : javascript, I have found this stack overflow solution how ever the filter is working form the var resultProductData = product_data.filter(function (product) { var date = new 2​) Do you plan in going in another programming speciality / language in the  Javascript - get array of dates between 2 dates. var range = getDates(new Date(), new Date().addDays(7)); I'd like "range" to be an array of date objects, one for each day between the two dates. The trick is that it should handle month and year boundaries as well.

Create an array of Dates between start date and end date in , In this tutorial we will be creating an array of Dates between a start date and an end date using JavaScript. Start and End Dates. Create two variables that will  I am trying to use your filter-records-between-two-dates.xls Excel 97-2003 Workbook *.xls and use the #num errors removal but the returned value is #NAME? instead, can you tell me what I am doing wrong?

Return all dates between two dates in an array in PHP , Given two dates (start date and end date) and the task is to return all the dates in an array. Example 1: In this example, use date interval class which stores fixed  Bottom line: Learn how to apply date filters with VBA. Includes examples for filtering for a range between two dates, date groups from filter drop-down list, dynamic dates in period, etc. The Excel file that contains the code can be downloaded below. This file contains code for filtering different data types and filter types.

Comments
  • Post your array of dates also.
  • Could you please add $scope.tempArray to the question for us to play around with your logic?
  • Have a look at Array.prototype.filter() to make your life easier.
  • @VicJordan posted it. it's the same as $scope.arrayOfEventData
  • How will this change the result?
  • @Andreas It gives us the array of events between the given two times.