getEndOfMonth function

DateTime getEndOfMonth(
  1. DateTime date
)

Gets the last day of the month containing the given date.

date - Any date within the desired month.

Returns a DateTime representing the last day of the month at 00:00:00.

Example:

final anyDay = DateTime(2024, 1, 15);
final lastDay = getEndOfMonth(anyDay);
print(lastDay); // 2024-01-31 00:00:00

Implementation

DateTime getEndOfMonth(DateTime date) => DateTime(date.year, date.month + 1, 0);