kl23
kl23

Reputation: 75

How to filter items consisting within a Date Range (from dateA to dateB) in Flutter/Dart?

I am trying to create a filter where I need to filter using a range of Dates.

for eg. from 2021-05-21 to 2021-08-31 . I got dateA = 2021-05-21, dateB = 2021-08-31.

Suppose I have a list of Dates and list of Items below respectively:

the format of Date is yyyy-mm-dd.

datesList =

[ "2021-05-07", 
"2021-06-09", //
"2021-05-12", 
"2021-08-12", // 
"2021-04-15", 
"2021-07-08", // 
"2021-05-02",
"2021-06-31", // 
"2021-08-18", // 
"2021-02-09" ] 

itemsList =

[ "Apple", 
"Mango",        // 
"Apple", 
"Pineapple,     // 
"Cinnamon", 
"Apple",        // 
"Mango",
"Banana",       // 
"Orange",       // 
"Orange" ]

Note: Here, for the 1st element in datesList for eg. "2021-05-07", the respective item is the 1st item in itemsList which is "Apple". i.e. both are having same indices, and both the lists are having same number of elements as well.

How to find only the items in a particular date range?

Let's say I need to filter from 2021-05-21 to 2021-08-31.

Then I should get only itemList[2], itemList[4], itemList[6], itemList[8], itemList[9] from the itemsList as Output. which is basically ["Mango", "Pineapple", "Apple", "Banana", "Orange"]

As only these items are present within this date range are needed to be filtered. (dateA = 2021-05-21, dateB = 2021-08-31.)

I couldn't even start as I am new in Flutter/Dart.

Kindly suggest me to proceed.

Upvotes: 0

Views: 6462

Answers (1)

jamesdlin
jamesdlin

Reputation: 90125

See How do I convert a date/time string to a DateTime object in Dart? for how to parse your date strings into DateTime objects. With that, you can iterate over you lists, compare each parsed DateTime against your start and end dates, and then add the corresponding item to list of results.

For example:

import 'package:intl/intl.dart';

/// Returns a list of items that have corresponding dates between [start]
/// and [end] (inclusive).
List<String> itemsBetweenDates({
  required List<String> dates,
  required List<String> items,
  required DateTime start,
  required DateTime end,
}) {
  assert(dates.length == items.length);

  var dateFormat = DateFormat('y-MM-dd');

  var output = <String>[];
  for (var i = 0; i < dates.length; i += 1) {
    var date = dateFormat.parse(dates[i], true);
    if (date.compareTo(start) >= 0 && date.compareTo(end) <= 0) {
      output.add(items[i]);
    }
  }
  return output;
}

void main() {
  var startDate = DateTime.utc(2021, 05, 21);
  var endDate = DateTime.utc(2021, 08, 31);

  var datesList = [
    "2021-05-07",
    "2021-06-09", //
    "2021-05-12",
    "2021-08-12", //
    "2021-04-15",
    "2021-07-08", //
    "2021-05-02",
    "2021-06-31", //
    "2021-08-18", //
    "2021-02-09",
  ];

  var itemsList = [
    "Apple",
    "Mango", //
    "Apple",
    "Pineapple", //
    "Cinnamon",
    "Apple", //
    "Mango",
    "Banana", //
    "Orange", //
    "Orange",
  ];

  print(itemsBetweenDates(
      dates: datesList, items: itemsList, start: startDate, end: endDate));
}

Note that the above assumes that the start and end dates are inclusive. If you want, say, the end date to be exclusive, change date.compareTo(end) <= 0 to date.compareTo(end) < 0.

Upvotes: 2

Related Questions