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

Commit d253208

Browse files
khashayarwesleycho
authored andcommitted
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 #2345
1 parent d784354 commit d253208

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/datepicker/datepicker.js

+5
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

+7
Original file line numberDiff line numberDiff line change
@@ -1547,11 +1547,18 @@ describe('datepicker directive', function () {
15471547
it('should be invalid initially', function() {
15481548
expect(inputEl.hasClass('ng-invalid')).toBeTruthy();
15491549
});
1550+
15501551
it('should be valid if model has been specified', function() {
15511552
$rootScope.date = new Date();
15521553
$rootScope.$digest();
15531554
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
15541555
});
1556+
1557+
it('should be valid if model value is a valid timestamp', function() {
1558+
$rootScope.date = Date.now();
1559+
$rootScope.$digest();
1560+
expect(inputEl.hasClass('ng-valid')).toBeTruthy();
1561+
});
15551562
});
15561563

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

0 commit comments

Comments
 (0)