How can I get all days of the week of a Datetime?
example: DateTime.utc(2022, 12, 21)
. The result should be:
mon(19)-tue(20)-wed(21)-thu(22)-fri(23)-sat(24)-sun(25)
CodePudding user response:
To get all weekdays for a given DateTime, you can use:
void main() {
print(getAllWeekDays(DateTime.now()));
}
List<DateTime> getAllWeekDays(DateTime datetime) {
return List.generate(7, (index) {
return datetime.subtract(Duration(days: datetime.weekday - index));
});
}
Prints:
[2022-12-18 11:46:53.333, 2022-12-19 11:46:53.333, 2022-12-20 11:46:53.333, 2022-12-21 11:46:53.333, 2022-12-22 11:46:53.333, 2022-12-23 11:46:53.333, 2022-12-24 11:46:53.333]
Note: this isn't a complete answer to get exactly what you want, as you'll need to format the DateTimes
.
But to do so, you can use the intl package.
Additionally, take a look at:
How do I format a date with Dart?