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

Commit

Permalink
fix(datepicker): use time-insensitive comparison for min/max.
Browse files Browse the repository at this point in the history
  • Loading branch information
jelbourn committed Jan 15, 2016
1 parent 3f1208b commit 0e334cd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/components/datepicker/datePicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,18 +367,21 @@
* @param {Date=} opt_date Date to check. If not given, defaults to the datepicker's model value.
*/
DatePickerCtrl.prototype.updateErrorState = function(opt_date) {
var date = opt_date || this.date;
// Force all dates to midnight in order to ignore the time portion.
var date = this.dateUtil.createDateAtMidnight(opt_date || this.date);

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

if (this.dateUtil.isValidDate(date)) {
if (this.dateUtil.isValidDate(this.minDate)) {
this.ngModelCtrl.$setValidity('mindate', date >= this.minDate);
var minDate = this.dateUtil.createDateAtMidnight(this.minDate);
this.ngModelCtrl.$setValidity('mindate', date >= minDate);
}

if (this.dateUtil.isValidDate(this.maxDate)) {
this.ngModelCtrl.$setValidity('maxdate', date <= this.maxDate);
var maxDate = this.dateUtil.createDateAtMidnight(this.maxDate);
this.ngModelCtrl.$setValidity('maxdate', date <= maxDate);
}

if (angular.isFunction(this.dateFilter)) {
Expand Down
34 changes: 34 additions & 0 deletions src/components/datepicker/datePicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,40 @@ describe('md-date-picker', function() {
expect(controller.ngModelCtrl.$error['maxdate']).toBe(true);
});

it('should ignore the time portion when comparing max-date', function() {
// Given that selected date is the same day as maxdate but at a later time.
pageScope.maxDate = new Date(2015, JAN, 1, 5, 30);
pageScope.myDate = new Date(2015, JAN, 1, 7, 30);
pageScope.$apply();

expect(controller.ngModelCtrl.$error['maxdate']).toBeFalsy();
});

it('should ignore the time portion when comparing min-date', function() {
// Given that selected date is the same day as mindate but at an earlier time.
pageScope.minDate = new Date(2015, JAN, 1, 5, 30);
pageScope.myDate = new Date(2015, JAN, 1);
pageScope.$apply();

expect(controller.ngModelCtrl.$error['mindate']).toBeFalsy();
});

it('should allow selecting a date exactly equal to the max-date', function() {
pageScope.maxDate = new Date(2015, JAN, 1);
pageScope.myDate = new Date(2015, JAN, 1);
pageScope.$apply();

expect(controller.ngModelCtrl.$error['maxdate']).toBeFalsy();
});

it('should allow selecting a date exactly equal to the min-date', function() {
pageScope.minDate = new Date(2015, JAN, 1);
pageScope.myDate = new Date(2015, JAN, 1);
pageScope.$apply();

expect(controller.ngModelCtrl.$error['mindate']).toBeFalsy();
});

describe('inside of a form element', function() {
var formCtrl;

Expand Down

0 comments on commit 0e334cd

Please sign in to comment.