getDaysInMonth function
Generate list of days in a month If full weeks, it will add prev/next month days
Implementation
List<Jiffy> getDaysInMonth(
Jiffy date,
int weekDayStart, {
bool fullWeeks = true,
}) {
final theDate = date.dateTime.copyWith(
hour: 0,
minute: 0,
second: 0,
millisecond: 0,
);
final List<Jiffy> days = [];
final int daysInMonth = DateUtils.getDaysInMonth(theDate.year, theDate.month);
final DateTime firstDayOfMonth = DateTime(theDate.year, theDate.month, 1);
if (fullWeeks) {
late int daysBefore;
if (weekDayStart == DateTime.sunday) {
daysBefore = firstDayOfMonth.weekday - DateTime.monday + 1;
} else {
daysBefore = firstDayOfMonth.weekday - DateTime.monday;
}
final DateTime prevMonth = firstDayOfMonth.subtract(Duration(days: daysBefore));
for (int i = 0; i < daysBefore; i++) {
days.add(Jiffy.parseFromDateTime(prevMonth.add(Duration(days: i))));
}
}
for (int i = 0; i < daysInMonth; i++) {
final DateTime day = firstDayOfMonth.copyWith(
day: i + 1,
);
days.add(Jiffy.parseFromDateTime(day));
}
if (fullWeeks) {
final DateTime lastDayOfMonth = DateTime(theDate.year, theDate.month, daysInMonth);
late int daysAfter;
if (weekDayStart == DateTime.sunday) {
daysAfter = DateTime.saturday - lastDayOfMonth.weekday;
} else {
daysAfter = DateTime.sunday - lastDayOfMonth.weekday;
}
final DateTime nextMonth = lastDayOfMonth.add(const Duration(days: 1));
for (int i = 0; i < daysAfter; i++) {
days.add(Jiffy.parseFromDateTime(nextMonth.add(Duration(days: i))));
}
}
return days;
}