relativeDateBucket function

String relativeDateBucket(
  1. DateTime date,
  2. DateTime today
)

Bucket label for date relative to today. Returns short label.

Counts calendar days from the year/month/day fields, ignoring the time of day and the UTC/local kind. Any date after today returns "older". Audited: 2026-06-12 11:26 EDT

Implementation

String relativeDateBucket(DateTime date, DateTime today) {
  // Compare date-only (drop the time component) so the bucket reflects calendar
  // days, not elapsed 24-hour spans — otherwise "this morning vs last night"
  // could misbucket. Buckets are checked nearest-first and are mutually
  // exclusive by the ascending day thresholds.
  // Bug fix: these were LOCAL midnights, and difference().inDays truncates
  // elapsed hours, so a span containing a 23-hour spring-forward day counted one
  // day short ("today" for yesterday). UTC has no DST, so every day is 24h and
  // the count equals the calendar-field difference.
  final DateTime d = DateTime.utc(date.year, date.month, date.day);
  final DateTime t = DateTime.utc(today.year, today.month, today.day);
  final int days = t.difference(d).inDays;
  if (days == 0) return _kToday;
  if (days == 1) return _kYesterday;
  if (days > 1 && days <= 7) return _kLast7Days;
  if (days > 7 && days <= 30) return _kLast30Days;
  return _kOlder;
}