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

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 #2345
  • Loading branch information
khashayar authored and wesleycho committed Mar 22, 2015
1 parent d784354 commit d253208
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 @@ -1547,11 +1547,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 d253208

Please sign in to comment.