From 8fea88879a42d248edbb107449aee7efad7fda15 Mon Sep 17 00:00:00 2001 From: Adel Gadllah Date: Mon, 31 Jan 2011 21:00:16 +0100 Subject: [PATCH] calendar: Fix prev/next buttons to not skip months When the current day does not exist in the next/prev month (like 31 Feb), the next/prev buttons end up skipping the month. Fix that by going to the last day of the month instead. https://bugzilla.gnome.org/show_bug.cgi?id=641067 --- js/ui/calendar.js | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/js/ui/calendar.js b/js/ui/calendar.js index 236c3ec67..07b9d291d 100644 --- a/js/ui/calendar.js +++ b/js/ui/calendar.js @@ -488,23 +488,45 @@ Calendar.prototype = { _onPrevMonthButtonClicked: function() { let newDate = new Date(this._selectedDate); - if (newDate.getMonth() == 0) { + let oldMonth = newDate.getMonth(); + if (oldMonth == 0) { newDate.setMonth(11); newDate.setFullYear(newDate.getFullYear() - 1); - } else { - newDate.setMonth(newDate.getMonth() - 1); + if (newDate.getMonth() != 11) { + let day = 32 - new Date(newDate.getFullYear() - 1, 11, 32).getDate(); + newDate = new Date(newDate.getFullYear() - 1, 11, day); + } } + else { + newDate.setMonth(oldMonth - 1); + if (newDate.getMonth() != oldMonth - 1) { + let day = 32 - new Date(newDate.getFullYear(), oldMonth - 1, 32).getDate(); + newDate = new Date(newDate.getFullYear(), oldMonth - 1, day); + } + } + this.setDate(newDate); }, - _onNextMonthButtonClicked: function() { + _onNextMonthButtonClicked: function() { let newDate = new Date(this._selectedDate); - if (newDate.getMonth() == 11) { + let oldMonth = newDate.getMonth(); + if (oldMonth == 11) { newDate.setMonth(0); newDate.setFullYear(newDate.getFullYear() + 1); - } else { - newDate.setMonth(newDate.getMonth() + 1); + if (newDate.getMonth() != 0) { + let day = 32 - new Date(newDate.getFullYear() + 1, 0, 32).getDate(); + newDate = new Date(newDate.getFullYear() + 1, 0, day); + } } + else { + newDate.setMonth(oldMonth + 1); + if (newDate.getMonth() != oldMonth + 1) { + let day = 32 - new Date(newDate.getFullYear(), oldMonth + 1, 32).getDate(); + newDate = new Date(newDate.getFullYear(), oldMonth + 1, day); + } + } + this.setDate(newDate); },