weeksInMonth method

int weeksInMonth()

weeksInMonth(): Calculates the number of weeks in the month of a DateTime.

Implementation

int weeksInMonth() {
  DateTime firstDayOfMonth = DateTime(year, month, 1);
  DateTime lastDayOfMonth = DateTime(year, month + 1, 0);
  int daysInMonth = lastDayOfMonth.day;

  int daysRemaining = daysInMonth - firstDayOfMonth.weekday + 1;
  int weeks = (daysRemaining / 7).ceil();

  return weeks;
}