Skip to content

Commit

Permalink
make logic more specific by checking daysInNewMonth vs currentDate at…
Browse files Browse the repository at this point in the history
… start of function
  • Loading branch information
mikerodonnell89 committed Jun 4, 2019
1 parent bece50d commit 2c256d2
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions library/src/lib/calendar/calendar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,17 +616,21 @@ export class CalendarComponent implements OnInit, OnDestroy, AfterViewChecked, C
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();
/*
set the date to the first for now, to prevent skipping a month
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
*/
this.date.setDate(1);
if (currentDate > daysInNewMonth) {
this.date.setDate(1);
}
// set the month
this.date.setMonth(month);
// get the number of days in this month
const daysInMonth = new Date(this.date.getFullYear(), this.date.getMonth() + 1, 0).getDate();
// restore the date to whichever number is lower, today's date or the number of days in this month
this.date.setDate(Math.min(currentDate, daysInMonth));
// 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 2c256d2

Please sign in to comment.