From 548eb989df4f30c191c4094544971a842af5dc35 Mon Sep 17 00:00:00 2001 From: Khashayar Hajian Date: Sat, 21 Feb 2015 21:09:09 +0100 Subject: [PATCH] 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 --- src/datepicker/datepicker.js | 5 +++++ src/datepicker/test/datepicker.spec.js | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/src/datepicker/datepicker.js b/src/datepicker/datepicker.js index bc2354f110..ee917c7500 100644 --- a/src/datepicker/datepicker.js +++ b/src/datepicker/datepicker.js @@ -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; diff --git a/src/datepicker/test/datepicker.spec.js b/src/datepicker/test/datepicker.spec.js index b673d162da..2b3517fb93 100644 --- a/src/datepicker/test/datepicker.spec.js +++ b/src/datepicker/test/datepicker.spec.js @@ -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() {