Reputation: 417
Is it possible to format a list of dates? I tried formatting it by formatting the list but got an error..
The argument type 'List' can't be assigned to the parameter type 'DateTime'.
var list = <DateTime>[];
DateTime start = DateTime(2018, 12, 30);
final end = DateTime(2022, 12, 31);
while (start.isBefore(end)) {
list.add(start);
start = start.add(const Duration(days: 1));
}
print(DateFormat("MM-dd-yyyy").format(list)); // The argument type 'List<DateTime>' can't be assigned to the parameter type 'DateTime'.
When I format the date before it's put in a list an error comes up saying you can't use isBefore on a string.
var list = <DateTime>[];
DateTime start = DateTime(2018, 12, 30);
var date = DateFormat("MM-dd-yyyy").format(start);
final end = DateTime(2022, 12, 31);
while (date.isBefore(end)) { //The method 'isBefore' isn't defined for the class 'String'.
list.add(start);
start = start.add(const Duration(days: 1));
}
Upvotes: 0
Views: 975
Reputation: 80944
Change the first code snippet to the following code:
var list = <String>[];
DateTime start = DateTime(2018, 12, 30);
final end = DateTime(2022, 12, 31);
while (start.isBefore(end)) {
var formatedData = DateFormat("MM-dd-yyyy").format(start)
list.add(formatedData);
start = start.add(const Duration(days: 1));
}
Since format()
method returns a String
then change the list to type String
and then inside the while loop, you can format the start
date and add it to the list.
Upvotes: 1