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

fix(dateparser): baseDate only care about dates #4767

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions src/dateparser/test/dateparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -203,6 +207,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() {
expect(dateParser.parse(123456, 'dd.MM.yyyy')).toBe(123456);
var date = new Date();
Expand Down