Published on

How to get any day of the week in Dart and Flutter

Authors
  • avatar
    Name
    Saad Bash

User DateTime's weekday getter.

  • 1 for Monday
  • 2 for Tuesday ...
  • 7 for Sunday
// Today
DateTime now = DateTime.now();

// Get most recent Monday
DateTime mon = DateTime(now.year, now.month, now.day - (now.weekday - 1));

// Get most recent Wednesday
DateTime wed = DateTime(now.year, now.month, now.day - (now.weekday - 3));

// Get Sunday
DateTime sun = DateTime(now.year, now.month, now.day - (now.weekday - 7));