Skip to content

Commit

Permalink
fix: month gets skipped when the current date is the 31st and the use…
Browse files Browse the repository at this point in the history
…r clicks next month button (#862)

* fix month skip bug

* make logic more specific by checking daysInNewMonth vs currentDate at start of function
  • Loading branch information
mikerodonnell89 committed Jun 18, 2019
1 parent aa0e701 commit 24d7b9f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions library/src/lib/calendar/calendar.component.ts
Expand Up @@ -707,7 +707,23 @@ export class CalendarComponent implements OnInit, OnDestroy, AfterViewChecked, C

/** @hidden */
setCurrentMonth(month: number) {
// get the current date of the month
const currentDate = this.date.getDate();
// get the number of days in the new month
const daysInNewMonth = new Date(this.date.getFullYear(), month + 1, 0).getDate();
/*
if the currentDate > daysInNewMonth, set the date to the first for now, to prevent skipping a month
in the event that the currentDate is 31 and the next month has 30 days
*/
if (currentDate > daysInNewMonth) {
this.date.setDate(1);
}
// set the month
this.date.setMonth(month);
// if currentDate > daysInNewMonth, restore the date to whichever number is lower, today's date or the number of days in this month
if (currentDate > daysInNewMonth) {
this.date.setDate(Math.min(currentDate, daysInNewMonth));
}
this.month = this.date.getMonth();
this.monthName = this.monthsFullName[this.date.getMonth()];
this.year = this.date.getFullYear();
Expand Down

0 comments on commit 24d7b9f

Please sign in to comment.