I want to get days between two dates in Flutter. as follows:
List<DateTime> getDays({DateTime start, DateTime end}) {
// TODO ...
}
List<DateTime> days = getDays(
start: DateTime.parse('2021-10-25 12:00:00'),
end: DateTime.parse('2021-10-28 12:00:00'),
);
The data in the returned list is as follows:
[
DateTime(), // 2021-10-25 12:00:00
DateTime(), // 2021-10-26 12:00:00
DateTime(), // 2021-10-27 12:00:00
]
How to implement the getDays
method. Thanks!!!
CodePudding user response:
You don't have to implement it. There's already one that does this.
void main() {
List<DateTime> getDays({
required DateTime start,
required DateTime end
}) {
final days = end.difference(start).inDays;
return [
for (int i = 0; i < days; i )
start.add(Duration(days: i))
];
}
List<DateTime> days = getDays(
start: DateTime.parse('2021-10-25 12:00:00'),
end: DateTime.parse('2021-10-28 12:00:00'),
);
print(days);
// [
// 2021-10-25 12:00:00.000,
// 2021-10-26 12:00:00.000,
// 2021-10-27 12:00:00.000
// ]
}