Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit e199349

Browse files
bekospkozlowski-opensource
authored andcommitted
fix(datepicker): Today button should not set time
Fixes #1726 Closes #1808
1 parent dea67b0 commit e199349

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/datepicker/datepicker.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,16 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
442442
});
443443

444444
scope.select = function( date ) {
445-
scope.dateSelection( date === 'today' ? new Date() : date);
445+
if (date === 'today') {
446+
var today = new Date();
447+
if (angular.isDate(ngModel.$modelValue)) {
448+
date = new Date(ngModel.$modelValue);
449+
date.setFullYear(today.getFullYear(), today.getMonth(), today.getDate());
450+
} else {
451+
date = new Date(today.setHours(0, 0, 0, 0));
452+
}
453+
}
454+
scope.dateSelection( date );
446455
};
447456

448457
var $popup = $compile(popupEl)(scope);

src/datepicker/test/datepicker.spec.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -1042,14 +1042,41 @@ describe('datepicker directive', function () {
10421042
expect(dropdownEl.find('li').length).toBe(2);
10431043
});
10441044

1045-
it('should have four buttons', function() {
1045+
it('should have three buttons', function() {
10461046
expect(buttons.length).toBe(3);
10471047

10481048
expect(buttons.eq(0).text()).toBe('Today');
10491049
expect(buttons.eq(1).text()).toBe('Clear');
10501050
expect(buttons.eq(2).text()).toBe('Done');
10511051
});
10521052

1053+
it('should have a button to set today date without altering time part', function() {
1054+
var today = new Date();
1055+
buttons.eq(0).click();
1056+
expect($rootScope.date.getFullYear()).toBe(today.getFullYear());
1057+
expect($rootScope.date.getMonth()).toBe(today.getMonth());
1058+
expect($rootScope.date.getDate()).toBe(today.getDate());
1059+
1060+
expect($rootScope.date.getHours()).toBe(15);
1061+
expect($rootScope.date.getMinutes()).toBe(30);
1062+
expect($rootScope.date.getSeconds()).toBe(0);
1063+
});
1064+
1065+
it('should have a button to set today date if blank', function() {
1066+
$rootScope.date = null;
1067+
$rootScope.$digest();
1068+
1069+
var today = new Date();
1070+
buttons.eq(0).click();
1071+
expect($rootScope.date.getFullYear()).toBe(today.getFullYear());
1072+
expect($rootScope.date.getMonth()).toBe(today.getMonth());
1073+
expect($rootScope.date.getDate()).toBe(today.getDate());
1074+
1075+
expect($rootScope.date.getHours()).toBe(0);
1076+
expect($rootScope.date.getMinutes()).toBe(0);
1077+
expect($rootScope.date.getSeconds()).toBe(0);
1078+
});
1079+
10531080
it('should have a button to clear value', function() {
10541081
buttons.eq(1).click();
10551082
expect($rootScope.date).toBe(null);

0 commit comments

Comments
 (0)