getLastDayOfMonth function

int getLastDayOfMonth(
  1. int month, {
  2. int? year,
})

Returns the last day of month.

month from 1 to 12. year if null uses year from DateTime.now .

Implementation

int getLastDayOfMonth(int month, {int? year}) {
  year ??= DateTime.now().year;

  var cursor = DateTime(year, month, 28, 12, 0, 0);

  while (true) {
    var next = cursor.add(Duration(days: 1));
    if (next.month != cursor.month) {
      return cursor.day;
    }
    cursor = next;
  }
}