daysInAMonth method

int daysInAMonth(
  1. int monthNum,
  2. int year
)

Returns the number of days in the given month and year. The months are indexed starting from 1 (January = 1, December = 12).

Implementation

int daysInAMonth(int monthNum, int year) {
  // Length of months for a non-leap year
  List<int> monthLengths = [
    31, // January
    28, // February (29 for leap years)
    31, // March
    30, // April
    31, // May
    30, // June
    31, // July
    31, // August
    30, // September
    31, // October
    30, // November
    31, // December
  ];

  // Adjust February days for leap years
  if (leapYear(year)) {
    monthLengths[1] = 29; // February in a leap year
  }

  return monthLengths[monthNum - 1]; // Return days in the specified month
}