1

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)

Ron
  • 5,900
  • 2
  • 20
  • 30
  • [How to create a Minimal, Reproducible Question](https://stackoverflow.com/help/minimal-reproducible-example) You should not just request a ready solution. SO is for helping solve specific errors, after you've shown your effort solving them – Ron Dec 21 '22 at 17:48

1 Answers1

1

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?

MendelG
  • 14,885
  • 4
  • 25
  • 52