How can I find element in list that only half-matches DateTime object in Flutter/Dart?
401
Solution 1
There is no such built-in method. You should create your own.
- Define a method that returns if two
DateTime
objects have the same date:
bool areSameDay(DateTime one, DateTime two) {
return one.day == two.day && one.month == two.month && one.year == two.year;
}
- Call
firstWhere
on the list:
final targetDate = DateTime(2021, 3, 12);
try {
final dateTime = dates.firstWhere((date) => areSameDay(date, targetDate));
// Found
} catch (_) {
// Not found
}
Also note that contains()
would not do even for complete matches. This is because it compares references to DateTime
objects and not their contents. To compare DateTime
object contents, use dateTime.compareTo(DateTime other)
Solution 2
You'll have to format your date. You can look up the options of the format of the dates here - Link
var newFormat = DateFormat("yyyy-MM-dd");
if (dates.singleWhere(
(element) =>
element.contains(newFormat.format(DateTime(2021, 3, 12))),
orElse: () => null) !=
null) {
print('Afermative');
} else {
print('Nope');
}
Solution 3
Try to use Jiffy package, it supports date querying. See below
var jiffy = Jiffy(DateTime(2021, 3, 12));
dates.forEach((date) {
var isSame = jiffy.isSame(date);
if (isSame) {
print('Afermative');
} else {
print('Nope');
}
});
Author by
RobbB
Updated on December 28, 2022Comments
-
RobbB over 1 year
I have a list that contains DateTime like so:
[2021-03-05 00:00:00.000, 2021-03-12 08:00:00.000Z, 2021-03-19 08:00:00.000Z, 2021-03-26 08:00:00.000Z, 2021-04-02 08:00:00.000Z, 2021-04-09 08:00:00.000Z, 2021-04-16 08:00:00.000Z, 2021-04-23 08:00:00.000Z, 2021-04-30 08:00:00.000Z, 2021-05-07 08:00:00.000Z, 2021-05-14 08:00:00.000Z, 2021-05-21 08:00:00.000Z, 2021-05-28 08:00:00.000Z, 2021-06-04 08:00:00.000Z, 2021-06-11 08:00:00.000Z]
I need the full DateTime list items, however I want to be able to query for "half-matches" like so:
if (dates.contains(DateTime(2021, 3, 12))) { print('Afermative'); } else { print('Nope'); }
Currently prints nope, obviously.
Is there a simple way of querying for such "half-matches" like above?
-
Alexey Inkin about 3 yearsNo such methods in DateTime. It means you should write your own to compare
.year
,.month
and.day
, then pass it tofirstWhere()
or alike. -
RobbB about 3 yearsSorry, please explain. I do not understand what you're saying.
-
Alexey Inkin about 3 yearsI explained in an answer.
-
-
RobbB about 3 yearsAhhh, i get it. This works, thanks for the input! I want to try one more method tomorrow with pub.dev/packages/dart_date
-
RobbB about 3 yearsLooks nice and simple, will try it out tomorrow. Thanks.
-
RobbB about 3 yearsHey Jama I could not get this to work, throws error DateTime is not a subtype of int (something like that, lost the error and went on, sorry).
-
Jama Mohamed about 3 yearsSorry, didn't realize your
dates
variable was a list ofDateTime
, I have now updated my answer -
Jama Mohamed about 3 yearsSorry, didn't realize that your
dates
variable was a list ofDateTime
, I have now updated my answer, to check if eachDateTime
in the list matches the target