isSameWeek method
Checks if two DateTime instances fall within the same week.
Example:
DateTime date1 = DateTime(2024, 1, 8);
DateTime date2 = DateTime(2024, 1, 12);
bool result = date1.isSameWeek(date2);
print(result); // Output: true
The isSameWeek
method compares two DateTime instances, ignoring the time component,
and determines if they fall within the same week.
Implementation
bool isSameWeek(DateTime b) {
final a = DateTime.utc(year, month, day);
b = DateTime.utc(b.year, b.month, b.day);
final diff = a.toUtc().difference(b.toUtc()).inDays;
if (diff.abs() >= 7) {
return false;
}
final min = a.isBefore(b) ? a : b;
final max = a.isBefore(b) ? b : a;
final result = max.weekday % 7 - min.weekday % 7 >= 0;
return result;
}