toRecentWeekday static method

DateTime toRecentWeekday(
  1. int weekday, [
  2. DateTime? initial
])

Implementation

static DateTime toRecentWeekday(int weekday, [DateTime? initial]) {
  // Get the initial date
  final today = initial ?? DateTime.now();

  // Calculate how many days to subtract to get the recent weekday
  int difference = today.weekday - weekday;

  // If the difference is negative, go back to the previous week
  if (difference < 0) difference += 7;

  // Subtract the difference to get the recent weekday
  return today.subtract(Duration(days: difference));
}