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

Commit 21b2297

Browse files
Foxandxsswesleycho
authored andcommitted
fix(dateparser): baseDate only care about dates
Closes #4767
1 parent 429ddc1 commit 21b2297

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

src/dateparser/dateparser.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,7 @@ angular.module('ui.bootstrap.dateparser', [])
162162
fields = {
163163
year: baseDate.getFullYear(),
164164
month: baseDate.getMonth(),
165-
date: baseDate.getDate(),
166-
hours: baseDate.getHours(),
167-
minutes: baseDate.getMinutes(),
168-
seconds: baseDate.getSeconds(),
169-
milliseconds: baseDate.getMilliseconds()
165+
date: baseDate.getDate()
170166
};
171167
} else {
172168
if (baseDate) {
@@ -185,9 +181,7 @@ angular.module('ui.bootstrap.dateparser', [])
185181
if (isValid(fields.year, fields.month, fields.date)) {
186182
if (angular.isDate(baseDate) && !isNaN(baseDate.getTime())) {
187183
dt = new Date(baseDate);
188-
dt.setFullYear(fields.year, fields.month, fields.date,
189-
fields.hours, fields.minutes, fields.seconds,
190-
fields.milliseconds || 0);
184+
dt.setFullYear(fields.year, fields.month, fields.date);
191185
} else {
192186
dt = new Date(fields.year, fields.month, fields.date,
193187
fields.hours, fields.minutes, fields.seconds,

src/dateparser/test/dateparser.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ describe('date parser', function() {
1010
expect(dateParser.parse(input, format)).toEqual(date);
1111
}
1212

13+
function expectBaseParse(input, format, baseDate, date) {
14+
expect(dateParser.parse(input, format, baseDate)).toEqual(date);
15+
}
16+
1317
describe('with custom formats', function() {
1418
it('should work correctly for `dd`, `MM`, `yyyy`', function() {
1519
expectParse('17.11.2013', 'dd.MM.yyyy', new Date(2013, 10, 17, 0));
@@ -219,6 +223,27 @@ describe('date parser', function() {
219223
});
220224
});
221225

226+
describe('base date', function() {
227+
var baseDate;
228+
229+
beforeEach(function() {
230+
baseDate = new Date(2010, 10, 10);
231+
});
232+
233+
it('should pre-initialize our date with a base date', function() {
234+
expect(expectBaseParse('2015', 'yyyy', baseDate, new Date(2015, 10, 10)));
235+
expect(expectBaseParse('1', 'M', baseDate, new Date(2010, 0, 10)));
236+
expect(expectBaseParse('1', 'd', baseDate, new Date(2010, 10, 1)));
237+
});
238+
239+
it('should ignore the base date when it is an invalid date', inject(function($log) {
240+
spyOn($log, 'warn');
241+
expect(expectBaseParse('30-12', 'dd-MM', new Date('foo'), new Date(1900, 11, 30)));
242+
expect(expectBaseParse('30-2015', 'dd-yyyy', 'I am a cat', new Date(2015, 0, 30)));
243+
expect($log.warn).toHaveBeenCalledWith('dateparser:', 'baseDate is not a valid date');
244+
}));
245+
});
246+
222247
it('should not parse non-string inputs', function() {
223248
expectParse(123456, 'dd.MM.yyyy', 123456);
224249
var date = new Date();

0 commit comments

Comments
 (0)