prevDay method
Returns the previous day.
This method calculates the day before the current DateTime object.
It handles month and year changes correctly.
isStartOfDay if true, returns the previous day at 00:00:00.
If false, keeps this value's wall-clock time on the previous calendar day
(not exactly 24 elapsed hours earlier, which differs across DST).
The result keeps this value's kind: a UTC input yields UTC (midnight UTC
when isStartOfDay is true), a local input yields local.
Example:
DateTime.utc(2026, 3, 8, 15).prevDay(); // 2026-03-07 00:00Z
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
DateTime prevDay({bool isStartOfDay = true}) {
// Bug fix: a 24h Duration step from midnight after a 23-hour DST day landed
// two days back, skipping the short day. Step by calendar day instead.
DateTime result = addCalendarDays(-1);
// A zone that skipped a whole date (Pacific/Apia, Dec 30, 2011) pushes the
// missing date forward onto this one, so "previous" would return today and a
// backward loop would never end. Step over the missing date.
if (result.day == day) {
result = addCalendarDays(-2);
}
// Truncate in the input's kind so a UTC value does not come back local.
return isStartOfDay ? result.startOfDayInKind : result;
}