From 21b2297aec179e747f45370f4c63177454fc1b08 Mon Sep 17 00:00:00 2001 From: Foxandxss Date: Wed, 28 Oct 2015 20:30:38 +0100 Subject: [PATCH] fix(dateparser): baseDate only care about dates Closes #4767 --- src/dateparser/dateparser.js | 10 ++-------- src/dateparser/test/dateparser.spec.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/dateparser/dateparser.js b/src/dateparser/dateparser.js index 43d0193ab9..732b509587 100644 --- a/src/dateparser/dateparser.js +++ b/src/dateparser/dateparser.js @@ -162,11 +162,7 @@ angular.module('ui.bootstrap.dateparser', []) fields = { year: baseDate.getFullYear(), month: baseDate.getMonth(), - date: baseDate.getDate(), - hours: baseDate.getHours(), - minutes: baseDate.getMinutes(), - seconds: baseDate.getSeconds(), - milliseconds: baseDate.getMilliseconds() + date: baseDate.getDate() }; } else { if (baseDate) { @@ -185,9 +181,7 @@ angular.module('ui.bootstrap.dateparser', []) if (isValid(fields.year, fields.month, fields.date)) { if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) { dt = new Date(baseDate); - dt.setFullYear(fields.year, fields.month, fields.date, - fields.hours, fields.minutes, fields.seconds, - fields.milliseconds || 0); + dt.setFullYear(fields.year, fields.month, fields.date); } else { dt = new Date(fields.year, fields.month, fields.date, fields.hours, fields.minutes, fields.seconds, diff --git a/src/dateparser/test/dateparser.spec.js b/src/dateparser/test/dateparser.spec.js index 2aaab67214..d76000a73e 100644 --- a/src/dateparser/test/dateparser.spec.js +++ b/src/dateparser/test/dateparser.spec.js @@ -10,6 +10,10 @@ describe('date parser', function() { expect(dateParser.parse(input, format)).toEqual(date); } + function expectBaseParse(input, format, baseDate, date) { + expect(dateParser.parse(input, format, baseDate)).toEqual(date); + } + describe('with custom formats', function() { it('should work correctly for `dd`, `MM`, `yyyy`', function() { expectParse('17.11.2013', 'dd.MM.yyyy', new Date(2013, 10, 17, 0)); @@ -219,6 +223,27 @@ describe('date parser', function() { }); }); + describe('base date', function() { + var baseDate; + + beforeEach(function() { + baseDate = new Date(2010, 10, 10); + }); + + it('should pre-initialize our date with a base date', function() { + expect(expectBaseParse('2015', 'yyyy', baseDate, new Date(2015, 10, 10))); + expect(expectBaseParse('1', 'M', baseDate, new Date(2010, 0, 10))); + expect(expectBaseParse('1', 'd', baseDate, new Date(2010, 10, 1))); + }); + + it('should ignore the base date when it is an invalid date', inject(function($log) { + spyOn($log, 'warn'); + expect(expectBaseParse('30-12', 'dd-MM', new Date('foo'), new Date(1900, 11, 30))); + expect(expectBaseParse('30-2015', 'dd-yyyy', 'I am a cat', new Date(2015, 0, 30))); + expect($log.warn).toHaveBeenCalledWith('dateparser:', 'baseDate is not a valid date'); + })); + }); + it('should not parse non-string inputs', function() { expectParse(123456, 'dd.MM.yyyy', 123456); var date = new Date();