diff --git a/public/js/components/Calendar/Month.js b/public/js/components/Calendar/Month.js index 64f7198f5..79fc1bf23 100644 --- a/public/js/components/Calendar/Month.js +++ b/public/js/components/Calendar/Month.js @@ -26,7 +26,7 @@ export default { this.syncOnNextChange = false; this.focusDate.set(this.date); } else { - this.focusDate.m += dir; + this.focusDate.moveMonthInDirection(dir) } this.$emit('change:range', { start: new Date(this.focusDate.y, this.focusDate.m, 1), diff --git a/public/js/composables/CalendarDate.js b/public/js/composables/CalendarDate.js index 3a69de7dc..ff7b2916f 100644 --- a/public/js/composables/CalendarDate.js +++ b/public/js/composables/CalendarDate.js @@ -1,6 +1,11 @@ import {user_locale} from "../plugin/Phrasen.js"; import CalendarDates from "./CalendarDates.js"; +const monthDayRanges = [] +for(let i = 1; i < 13; i++) { + monthDayRanges.push(new Date(1995, i, 0).getDate()) +} + class CalendarDate { constructor(y, m, d) { this.weekStart = CalendarDate.getWeekStart(); @@ -210,6 +215,15 @@ class CalendarDate { cleanup(){ if(this.watchLocale && this.watchLocale.stop) this.watchLocale.stop(); // TODO: ? } + moveMonthInDirection (dir) { + // avoid setting date in wrong month if we try to create a date which does not exist like 30th of february + const newM = this.m + dir + const maxDaysTarget = monthDayRanges[newM] + + if (this.d > maxDaysTarget) this.d = maxDaysTarget + + this.m = newM // calls _clean which sets a new Date + } } /** * Returns the weekday number (Date.getDay()) on which the week starts depending on the locale.