previousMonth property
DateTime
get
previousMonth
Returns a DateTime instance representing the exact date of the previous month.
Example:
DateTime currentDate = DateTime(2024, 1, 8);
DateTime previousMonthDate = currentDate.previousMonth;
print(previousMonthDate); // Output: 2023-12-08 00:00:00
The previousMonth
extension calculates and returns a new DateTime instance
with the same day and time as the original date but adjusted to the exact date of the previous month.
Implementation
DateTime get previousMonth {
var year = this.year;
var month = this.month;
if (month == 1) {
year--;
month = 12;
} else {
month--;
}
return DateTime(year, month);
}