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

Commit

Permalink
fix(dateparser): baseDate only care about dates
Browse files Browse the repository at this point in the history
Closes #4767
  • Loading branch information
Foxandxss authored and wesleycho committed Oct 28, 2015
1 parent 429ddc1 commit 21b2297
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
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 @@ -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();
Expand Down

0 comments on commit 21b2297

Please sign in to comment.