isSameWeekAs method

bool isSameWeekAs(
  1. DateTime other
)

Checks if this DateTime occurs within the same week as another DateTime, regardless of time zone. Considers a week to start on Monday.

Implementation

bool isSameWeekAs(DateTime other) {
  final thisNoonUtc = DateTime.utc(year, month, day, 12);
  final otherNoonUtc = DateTime.utc(other.year, other.month, other.day, 12);

  if (thisNoonUtc.difference(otherNoonUtc).inDays.abs() >= 7) return false;

  // Find the start of the week (Monday) for both dates
  final startOfWeekThis =
      thisNoonUtc.subtract(Duration(days: thisNoonUtc.weekday - 1));
  final startOfWeekOther =
      otherNoonUtc.subtract(Duration(days: otherNoonUtc.weekday - 1));

  return startOfWeekThis.isAtSameMomentAs(startOfWeekOther);
}