diff --git a/src/dateparser/dateparser.js b/src/dateparser/dateparser.js index eb323623b6..fea1b50d75 100644 --- a/src/dateparser/dateparser.js +++ b/src/dateparser/dateparser.js @@ -125,7 +125,7 @@ angular.module('ui.bootstrap.dateparser', []) if ( results && results.length ) { var fields, dt; - if (baseDate) { + if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) { fields = { year: baseDate.getFullYear(), month: baseDate.getMonth(), diff --git a/src/dateparser/test/dateparser.spec.js b/src/dateparser/test/dateparser.spec.js index 7ed6bb0e58..37c2bc972a 100644 --- a/src/dateparser/test/dateparser.spec.js +++ b/src/dateparser/test/dateparser.spec.js @@ -162,6 +162,14 @@ describe('date parser', function () { expect(dateParser.parse('31-04-2013', 'dd-MM-yyyy')).toBeUndefined(); expect(dateParser.parse('November 31, 2013', 'MMMM d, yyyy')).toBeUndefined(); }); + + it('should work when base date is a string', function() { + expect(dateParser.parse('01-02-2034', 'dd-MM-yyyy', '05-06-2078')).toEqual(new Date(2034, 1, 1)); + }); + + it('should work when base date is an invalid date', function() { + expect(dateParser.parse('30-12-2015', 'dd-MM-yyyy', new Date('foo'))).toEqual(new Date(2015, 11, 30)); + }); }); it('should not parse non-string inputs', function() {