lastDayOfMonth property

DateTime get lastDayOfMonth

Returns a DateTime instance representing the last day of the month of this DateTime.

Example:

DateTime currentDate = DateTime(2022, 1, 8);
DateTime lastDayOfMonth = currentDate.lastDayOfMonth;
print(lastDayOfMonth);  // Output: 2022-01-31 00:00:00.000

The lastDayOfMonth getter calculates and returns a new DateTime instance with the same year and month as the original date but adjusted to the last day of that month.

Implementation

DateTime get lastDayOfMonth {
  var beginningNextMonth =
      (month < 12) ? DateTime(year, month + 1, 1) : DateTime(year + 1, 1, 1);
  return beginningNextMonth.subtract(const Duration(days: 1));
}