Skip to content

Commit 548eb98

Browse files
committed
fix(datepicker): ng-model value can be a timestamp
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
1 parent 001c9ef commit 548eb98

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/datepicker/datepicker.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,11 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
502502
}
503503

504504
function parseDate(viewValue) {
505+
if (angular.isNumber(viewValue)) {
506+
// presumably timestamp to date object
507+
viewValue = new Date(viewValue);
508+
}
509+
505510
if (!viewValue) {
506511
ngModel.$setValidity('date', true);
507512
return null;

src/datepicker/test/datepicker.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,11 +1506,18 @@ describe('datepicker directive', function () {
15061506
it('should be invalid initially', function() {
15071507
expect(inputEl.hasClass('ng-invalid')).toBeTruthy();
15081508
});
1509+
15091510
it('should be valid if model has been specified', function() {
15101511
$rootScope.date = new Date();
15111512
$rootScope.$digest();
15121513
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
15131514
});
1515+
1516+
it('should be valid if model value is a valid timestamp', function() {
1517+
$rootScope.date = Date.now();
1518+
$rootScope.$digest();
1519+
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
1520+
});
15141521
});
15151522

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

0 commit comments

Comments
 (0)