isYesterday method
Returns true if this DateTime is the calendar day before now.
Compares date-only (year/month/day), so the time component is irrelevant:
00:01 on the prior day still counts as yesterday. Pass now to pin the
clock for deterministic tests.
DST-safe (see isTomorrow): yesterday is found by stepping calendar fields, so a 23-hour or 25-hour local day cannot shift the answer.
Example:
final now = DateTime(2024, 3, 1);
DateTime(2024, 2, 29).isYesterday(now: now); // true (leap day)
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
bool isYesterday({DateTime? now}) {
// Bug fix: `now.subtract(Duration(days: 1))` skipped the 23-hour day just
// after midnight and stayed on the same day late on a 25-hour day.
final DateTime yesterday = _shiftCalendarDay(now ?? DateTime.now(), -1);
return isSameDateOnly(yesterday);
}