Skip to content

Commit 7afd292

Browse files
committed
fix: fallback to now if date is not valid when generating weeks array
1 parent 300ba49 commit 7afd292

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/lib/day-calendar/day-calendar.service.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,26 @@ export class DayCalendarService {
5151
}
5252

5353
generateMonthArray(config: IDayCalendarConfigInternal, month: Moment, selected: Moment[]): IDay[][] {
54+
const parsedMonth = month.isValid() ? month.clone() : moment();
5455
let monthArray: IDay[][] = [];
5556
const firstDayOfWeekIndex = this.DAYS.indexOf(config.firstDayOfWeek);
56-
const firstDayOfBoard = month.clone().startOf('month');
57+
const firstDayOfBoard = parsedMonth.clone().startOf('month');
5758

5859
while (firstDayOfBoard.day() !== firstDayOfWeekIndex) {
5960
firstDayOfBoard.subtract(1, 'day');
6061
}
6162

6263
const current = firstDayOfBoard.clone();
63-
const prevMonth = month.clone().subtract(1, 'month');
64-
const nextMonth = month.clone().add(1, 'month');
64+
const prevMonth = parsedMonth.clone().subtract(1, 'month');
65+
const nextMonth = parsedMonth.clone().add(1, 'month');
6566
const today = moment();
6667

6768
const daysOfCalendar: IDay[] = this.utilsService.createArray(42)
6869
.reduce((array: IDay[]) => {
6970
array.push({
7071
date: current.clone(),
7172
selected: !!selected.find(selectedDay => current.isSame(selectedDay, 'day')),
72-
currentMonth: current.isSame(month, 'month'),
73+
currentMonth: current.isSame(parsedMonth, 'month'),
7374
prevMonth: current.isSame(prevMonth, 'month'),
7475
nextMonth: current.isSame(nextMonth, 'month'),
7576
currentDay: current.isSame(today, 'day'),

0 commit comments

Comments
 (0)