lastDayOfMonth static method

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

Implementation

static DateTime lastDayOfMonth({int? month, int? year}) {
  DateTime now = new DateTime.now();
  int targetMonth = month == null ? now.month : month;
  int targetYear = year == null ? now.year : year;
  if(targetMonth == 12){
    targetYear = targetYear + 1;
    targetMonth = 1;
  }
  else{
    targetMonth = targetMonth + 1;
  }
  DateTime lastDayOfMonth = new DateTime(targetYear, targetMonth, 0);
  return lastDayOfMonth;
}