Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 0e334cd

Browse files
committed
fix(datepicker): use time-insensitive comparison for min/max.
1 parent 3f1208b commit 0e334cd

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/components/datepicker/datePicker.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,18 +367,21 @@
367367
* @param {Date=} opt_date Date to check. If not given, defaults to the datepicker's model value.
368368
*/
369369
DatePickerCtrl.prototype.updateErrorState = function(opt_date) {
370-
var date = opt_date || this.date;
370+
// Force all dates to midnight in order to ignore the time portion.
371+
var date = this.dateUtil.createDateAtMidnight(opt_date || this.date);
371372

372373
// Clear any existing errors to get rid of anything that's no longer relevant.
373374
this.clearErrorState();
374375

375376
if (this.dateUtil.isValidDate(date)) {
376377
if (this.dateUtil.isValidDate(this.minDate)) {
377-
this.ngModelCtrl.$setValidity('mindate', date >= this.minDate);
378+
var minDate = this.dateUtil.createDateAtMidnight(this.minDate);
379+
this.ngModelCtrl.$setValidity('mindate', date >= minDate);
378380
}
379381

380382
if (this.dateUtil.isValidDate(this.maxDate)) {
381-
this.ngModelCtrl.$setValidity('maxdate', date <= this.maxDate);
383+
var maxDate = this.dateUtil.createDateAtMidnight(this.maxDate);
384+
this.ngModelCtrl.$setValidity('maxdate', date <= maxDate);
382385
}
383386

384387
if (angular.isFunction(this.dateFilter)) {

src/components/datepicker/datePicker.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,40 @@ describe('md-date-picker', function() {
155155
expect(controller.ngModelCtrl.$error['maxdate']).toBe(true);
156156
});
157157

158+
it('should ignore the time portion when comparing max-date', function() {
159+
// Given that selected date is the same day as maxdate but at a later time.
160+
pageScope.maxDate = new Date(2015, JAN, 1, 5, 30);
161+
pageScope.myDate = new Date(2015, JAN, 1, 7, 30);
162+
pageScope.$apply();
163+
164+
expect(controller.ngModelCtrl.$error['maxdate']).toBeFalsy();
165+
});
166+
167+
it('should ignore the time portion when comparing min-date', function() {
168+
// Given that selected date is the same day as mindate but at an earlier time.
169+
pageScope.minDate = new Date(2015, JAN, 1, 5, 30);
170+
pageScope.myDate = new Date(2015, JAN, 1);
171+
pageScope.$apply();
172+
173+
expect(controller.ngModelCtrl.$error['mindate']).toBeFalsy();
174+
});
175+
176+
it('should allow selecting a date exactly equal to the max-date', function() {
177+
pageScope.maxDate = new Date(2015, JAN, 1);
178+
pageScope.myDate = new Date(2015, JAN, 1);
179+
pageScope.$apply();
180+
181+
expect(controller.ngModelCtrl.$error['maxdate']).toBeFalsy();
182+
});
183+
184+
it('should allow selecting a date exactly equal to the min-date', function() {
185+
pageScope.minDate = new Date(2015, JAN, 1);
186+
pageScope.myDate = new Date(2015, JAN, 1);
187+
pageScope.$apply();
188+
189+
expect(controller.ngModelCtrl.$error['mindate']).toBeFalsy();
190+
});
191+
158192
describe('inside of a form element', function() {
159193
var formCtrl;
160194

0 commit comments

Comments
 (0)