diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index 692696376e..8e1a99a212 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -228,6 +228,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.position']) }; scope.move = function(direction) { var step = datepickerCtrl.modes[mode].step; + selected.setDate(1); // Avoid having a day that doesn't exist on the objective month. selected.setMonth( selected.getMonth() + direction * (step.months || 0) ); selected.setFullYear( selected.getFullYear() + direction * (step.years || 0) ); refill(); diff --git a/src/datepicker/test/datepicker.spec.js b/src/datepicker/test/datepicker.spec.js index ec679e1c85..b242459742 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -219,6 +219,44 @@ describe('datepicker directive', function () { expectSelectedElement( 0, 4 ); }); + it('issue 1697 - moves to the next month & render correctly when the selected day doesnt exist on the next month', function() { + $rootScope.date = new Date('January 31, 2010 15:30:00'); + $rootScope.$digest(); + + clickNextButton(); + + expect(getTitle()).toBe('February 2010'); + expect(getLabels()).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']); + expect(getOptions()).toEqual([ + ['31', '01', '02', '03', '04', '05', '06'], + ['07', '08', '09', '10', '11', '12', '13'], + ['14', '15', '16', '17', '18', '19', '20'], + ['21', '22', '23', '24', '25', '26', '27'], + ['28', '01', '02', '03', '04', '05', '06'] + ]); + + expectSelectedElement( 0, 0 ); + }); + + it('issue 1697 - moves to the previous month & render correctly when the selected day doesnt exist on the previous month', function() { + $rootScope.date = new Date('March 31, 2010 15:30:00'); + $rootScope.$digest(); + + clickPreviousButton(); + + expect(getTitle()).toBe('February 2010'); + expect(getLabels()).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']); + expect(getOptions()).toEqual([ + ['31', '01', '02', '03', '04', '05', '06'], + ['07', '08', '09', '10', '11', '12', '13'], + ['14', '15', '16', '17', '18', '19', '20'], + ['21', '22', '23', '24', '25', '26', '27'], + ['28', '01', '02', '03', '04', '05', '06'] + ]); + + expectSelectedElement( null, null ); + }); + it('updates the model only when a day is clicked in the `next` month', function() { clickNextButton(); expect($rootScope.date).toEqual(new Date('September 30, 2010 15:30:00'));