0

I have an array of dates array can be very large around 10,000 objects like as

[{"date":"2019-01-23T11:01:50.719Z","lat":28.591072559026497,"lng":77.32183123723946},{"date":"2019-01-23T11:01:58.313Z","lat":28.591143596331815,"lng":77.32190503671627},{"date":"2019-01-23T11:04:13.272Z","lat":28.59104724711076,"lng":77.32189079414962},{"date":"2019-01-23T11:07:21.256Z","lat":28.591160550128887,"lng":77.32204036332226}]

I am trying to filter above Large Array by Date between start and end date. Since one date has many date with time. Like as

22/01/19 11:00, 22/01/19 12:00,22/01/19 1:00,22/01/19 2:00........... 24/01/19 1:00, 24/01/19 2:00,24/01/19 3:00 .......... 25/01/19 1:00

Suppose my start date is 22/01/19 and End date is 24/01/19. So array will be start from 22/01/19 2:00 (highest time) to 24/01/19 3:00. I am trying to create the logic but unable to do it. Kindly help me please.

3
  • what is start date and end date? Commented Jan 24, 2019 at 8:05
  • 1
    Hello, and welcome to StackOverflow. Unlike other pages StackOverflow is not a "give me free code" page. We will help you after you have shown minimal effort. To help you a bit I would have a look at the javascript '.filter()' method. Also this question might help you stackoverflow.com/questions/48227286/… Commented Jan 24, 2019 at 8:06
  • You can check developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… also if you don't want to make date objects of the dates then ISO 8601 date comparison is equivalent to a string comparison if the time zones are the same. Commented Jan 24, 2019 at 8:12

2 Answers 2

2

Here. Actually a one-liner on the process of filtering on its own.

const data = [{
  "date": "2019-01-23T11:01:50.719Z",
  "lat": 28.591072559026497,
  "lng": 77.32183123723946
}, {
  "date": "2019-01-22T11:01:58.313Z",
  "lat": 28.591143596331815,
  "lng": 77.32190503671627
}, {
  "date": "2019-01-24T11:04:13.272Z",
  "lat": 28.59104724711076,
  "lng": 77.32189079414962
}, {
  "date": "2019-01-25T11:07:21.256Z",
  "lat": 28.591160550128887,
  "lng": 77.32204036332226
}]

let startDate = new Date("2019-01-22T02:00:00")
let endDate = new Date("2019-01-24T03:00:00")
let result = data.filter(d => new Date(d.date) >= startDate && new Date(d.date) <= endDate)
console.log(result)

Sign up to request clarification or add additional context in comments.

Comments

1

you can use filter for filter the data and compare data using moment for example:

const filteredDates = dates.filter(date => {
 const date = moment(date)
 const start = moment(start);
 const end = moment(end);
 if(date > start && date < end){
  return true;
 }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.