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

fix(dateParser): handle cases where baseDate is an Invalid Date #3817

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
8 changes: 5 additions & 3 deletions src/dateparser/dateparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ angular.module('ui.bootstrap.dateparser', [])
results = input.match(regex);

if ( results && results.length ) {
var fields, dt;
var defaultFields = { year: 1900, month: 0, date: 1, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 },
fields = {}, dt;
if (baseDate) {
fields = {
year: baseDate.getFullYear(),
Expand All @@ -135,9 +136,10 @@ angular.module('ui.bootstrap.dateparser', [])
seconds: baseDate.getSeconds(),
milliseconds: baseDate.getMilliseconds()
};
} else {
fields = { year: 1900, month: 0, date: 1, hours: 0, minutes: 0, seconds: 0, milliseconds: 0 };
}
angular.forEach(defaultFields, function (v, k) {
fields[k] = fields[k] || v;
});

for( var i = 1, n = results.length; i < n; i++ ) {
var mapper = map[i-1];
Expand Down
6 changes: 6 additions & 0 deletions src/dateparser/test/dateparser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ describe('date parser', function () {
expectParse('22.March.15.22:33:4', 'd.MMMM.yy.HH:mm:s', new Date(2015, 2, 22, 22, 33, 4));
expectParse('22.March.15.22:3:4', 'd.MMMM.yy.HH:m:s', new Date(2015, 2, 22, 22, 3, 4));
});

it('should work when baseDate is an Invalid Date object', function () {
var baseDate = new Date('28/11/198'); // value of the input field just before entering last digit
var parsed = dateParser.parse('28/11/1986', 'dd/MM/yyyy', baseDate);
expect(parsed).toEqual(new Date(1986, 10, 28));
});
});

describe('with predefined formats', function() {
Expand Down