Skip to content

Commit

Permalink
fix(datepicker): ng-model value can be a timestamp
Browse files Browse the repository at this point in the history
Accept a number of milliseconds since 01.01.1970 as a valid value
for `ng-model`:
- change parseDate() to handle timestamp values
- add the test to `datepicker.spec.js`

Closes angular-ui#2345
  • Loading branch information
khashayar committed Mar 8, 2015
1 parent 001c9ef commit 548eb98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
}

function parseDate(viewValue) {
if (angular.isNumber(viewValue)) {
// presumably timestamp to date object
viewValue = new Date(viewValue);
}

if (!viewValue) {
ngModel.$setValidity('date', true);
return null;
Expand Down
7 changes: 7 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1506,11 +1506,18 @@ describe('datepicker directive', function () {
it('should be invalid initially', function() {
expect(inputEl.hasClass('ng-invalid')).toBeTruthy();
});

it('should be valid if model has been specified', function() {
$rootScope.date = new Date();
$rootScope.$digest();
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
});

it('should be valid if model value is a valid timestamp', function() {
$rootScope.date = Date.now();
$rootScope.$digest();
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
});
});

describe('use with `ng-change` directive', function() {
Expand Down

0 comments on commit 548eb98

Please sign in to comment.