rameez khan
rameez khan

Reputation: 359

Flutter filter arrays which come between 2 days

I have 2 data lists. From first list I can find my 2 dates and time.

First data list look like this

"data1": [
        {

            "tripType": "active",
            "dateStart": "2021-05-31T03:02:38.000Z",
            "dateEnd": "2021-05-31T03:51:33.000Z",
            "start": {
                "lon": 66.975478,
                "lat": 24.949758,
            },
            "end": {
                "lon": 67.036706,
                "lat": 24.827635,
            }
         }];

On second list I have multiple arrays in which there are date and time. I want to filter this list and show only those arrays which date and time comes between on my first data starting and end time.

My second list look like this

"data2": [
    {
        "id": "3cec02d7-4938-418a-bcdd-c6043154a966",
        "eventDate": "2021-06-01T15:54:01.000Z",
        "creationDate": "2021-06-01T15:54:06.000Z",
        "modifiedDate": "2021-06-01T15:54:06.000Z",
        "eventClass": "overspeedevent",
        "eventType": "start",

    },
    {
        "id": "3cec02d7-4938-418a-bcdd-c6043154a966",
        "eventDate": "2021-07-01T13:54:01.000Z",
        "creationDate": "2021-06-01T13:54:06.000Z",
        "modifiedDate": "2021-06-01T13:54:06.000Z",
        "eventClass": "overspeedevent",
        "eventType": "end",

    },
    {
        "id": "3cec02d7-4938-418a-bcdd-c6043154a966",
        "eventDate": "2021-06-01T15:54:01.000Z",
        "creationDate": "2021-06-01T15:54:06.000Z",
        "modifiedDate": "2021-06-01T15:54:06.000Z",
        "eventClass": "overspeedevent",
        "eventType": "start",

    }];
    

You can see in data1 I have dateStart and dateEnd What i need is i need to find data2 between these dates in data2 i have eventDate I need to show or filter this array and show only those array which eventDate comes between dateStart and dateEnd

Upvotes: 0

Views: 456

Answers (1)

Saeid Doroudi
Saeid Doroudi

Reputation: 1245

you can iterate over data2 and check the date with the compareTo method of date after converting it to date

 var startDate = DateTime.parse(data1[0]["dateStart"].toString());
 var endDate = DateTime.parse(data1[0]["dateEnd"].toString());
 for(var item in data2){
   var itemDate = DateTime.parse(item["eventDate"].toString());

   if(itemDate.compareTo(startDate) > 0 && itemDate.compareTo(endDate) < 0) {
     print(item["id"]);
   }
 }

Upvotes: 2

Related Questions