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

Commit beabb4a

Browse files
committed
fix(datepicker): correctly display pre-100 years
- Fix display of dates in datepicker for years less than 100 Closes #4812 Fixes #4032
1 parent fc61e17 commit beabb4a

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/dateparser/dateparser.js

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ angular.module('ui.bootstrap.dateparser', [])
246246
dt = new Date(fields.year, fields.month, fields.date,
247247
fields.hours, fields.minutes, fields.seconds,
248248
fields.milliseconds || 0);
249+
dt.setFullYear(fields.year);
249250
}
250251
}
251252

src/dateparser/test/dateparser.spec.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
describe('date parser', function() {
2-
var dateParser;
2+
var dateParser, oldDate;
33

44
beforeEach(module('ui.bootstrap.dateparser'));
55
beforeEach(inject(function (uibDateParser) {
66
dateParser = uibDateParser;
7+
oldDate = new Date(1, 2, 6);
8+
oldDate.setFullYear(1);
79
}));
810

911
function expectParse(input, format, date) {
@@ -23,6 +25,7 @@ describe('date parser', function() {
2325
expectParse('10.01/1983', 'dd.MM/yyyy', new Date(1983, 0, 10, 0));
2426
expectParse('11-09-1980', 'MM-dd-yyyy', new Date(1980, 10, 9, 0));
2527
expectParse('2011/02/05', 'yyyy/MM/dd', new Date(2011, 1, 5, 0));
28+
expectParse('0001/03/06', 'yyyy/MM/dd', oldDate);
2629
});
2730

2831
it('should work correctly for `yy`', function() {
@@ -48,13 +51,15 @@ describe('date parser', function() {
4851
expectParse('05-March-1980', 'dd-MMMM-yyyy', new Date(1980, 2, 5, 0));
4952
expectParse('February/05/1980', 'MMMM/dd/yyyy', new Date(1980, 1, 5, 0));
5053
expectParse('1949/December/20', 'yyyy/MMMM/dd', new Date(1949, 11, 20, 0));
54+
expectParse('0001/March/06', 'yyyy/MMMM/dd', oldDate);
5155
});
5256

5357
it('should work correctly for `MMM`', function() {
5458
expectParse('30.Sep.10', 'dd.MMM.yy', new Date(2010, 8, 30, 0));
5559
expectParse('02-May-11', 'dd-MMM-yy', new Date(2011, 4, 2, 0));
5660
expectParse('Feb/05/1980', 'MMM/dd/yyyy', new Date(1980, 1, 5, 0));
5761
expectParse('1955/Feb/05', 'yyyy/MMM/dd', new Date(1955, 1, 5, 0));
62+
expectParse('0001/Mar/06', 'yyyy/MMM/dd', oldDate);
5863
});
5964

6065
it('should work correctly for `M`', function() {
@@ -73,13 +78,15 @@ describe('date parser', function() {
7378
expectParse('2/05/1980', 'M!/dd/yyyy', new Date(1980, 1, 5, 0));
7479
expectParse('1955/2/05', 'yyyy/M!/dd', new Date(1955, 1, 5, 0));
7580
expectParse('02-5-11', 'dd-M!-yy', new Date(2011, 4, 2, 0));
81+
expectParse('0001/3/06', 'yyyy/M!/dd', oldDate);
7682

7783
expectParse('08/11/2013', 'M!/dd/yyyy', new Date(2013, 7, 11, 0));
7884
expectParse('07.11.05', 'dd.M!.yy', new Date(2005, 10, 7, 0));
7985
expectParse('02-05-11', 'dd-M!-yy', new Date(2011, 4, 2, 0));
8086
expectParse('02/05/1980', 'M!/dd/yyyy', new Date(1980, 1, 5, 0));
8187
expectParse('1955/02/05', 'yyyy/M!/dd', new Date(1955, 1, 5, 0));
8288
expectParse('02-05-11', 'dd-M!-yy', new Date(2011, 4, 2, 0));
89+
expectParse('0001/03/06', 'yyyy/M!/dd', oldDate);
8390
});
8491

8592
it('should work correctly for `d`', function() {
@@ -88,6 +95,7 @@ describe('date parser', function() {
8895
expectParse('February/5/1980', 'MMMM/d/yyyy', new Date(1980, 1, 5, 0));
8996
expectParse('1955/February/5', 'yyyy/MMMM/d', new Date(1955, 1, 5, 0));
9097
expectParse('11-08-13', 'd-MM-yy', new Date(2013, 7, 11, 0));
98+
expectParse('0001/03/6', 'yyyy/MM/d', oldDate);
9199
});
92100

93101
it('should work correctly for `d!`', function() {
@@ -96,12 +104,14 @@ describe('date parser', function() {
96104
expectParse('February/5/1980', 'MMMM/d!/yyyy', new Date(1980, 1, 5, 0));
97105
expectParse('1955/February/5', 'yyyy/MMMM/d!', new Date(1955, 1, 5, 0));
98106
expectParse('11-08-13', 'd!-MM-yy', new Date(2013, 7, 11, 0));
107+
expectParse('0001/03/6', 'yyyy/MM/d!', oldDate);
99108

100109
expectParse('17.November.13', 'd!.MMMM.yy', new Date(2013, 10, 17, 0));
101110
expectParse('08-March-1991', 'd!-MMMM-yyyy', new Date(1991, 2, 8, 0));
102111
expectParse('February/05/1980', 'MMMM/d!/yyyy', new Date(1980, 1, 5, 0));
103112
expectParse('1955/February/05', 'yyyy/MMMM/d!', new Date(1955, 1, 5, 0));
104113
expectParse('11-08-13', 'd!-MM-yy', new Date(2013, 7, 11, 0));
114+
expectParse('0001/03/06', 'yyyy/MM/d!', oldDate);
105115
});
106116

107117
it('should work correctly for `EEEE`', function() {

src/datepicker/datepicker.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,11 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
280280
};
281281

282282
this.compare = function(date1, date2) {
283-
return (new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()) - new Date(date2.getFullYear(), date2.getMonth(), date2.getDate()));
283+
var _date1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
284+
var _date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
285+
_date1.setFullYear(date1.getFullYear());
286+
_date2.setFullYear(date2.getFullYear());
287+
return _date1 - _date2;
284288
};
285289

286290
function getISO8601WeekNumber(date) {
@@ -343,7 +347,11 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
343347
};
344348

345349
this.compare = function(date1, date2) {
346-
return new Date(date1.getFullYear(), date1.getMonth()) - new Date(date2.getFullYear(), date2.getMonth());
350+
var _date1 = new Date(date1.getFullYear(), date1.getMonth());
351+
var _date2 = new Date(date2.getFullYear(), date2.getMonth());
352+
_date1.setFullYear(date1.getFullYear());
353+
_date2.setFullYear(date2.getFullYear());
354+
return _date1 - _date2;
347355
};
348356

349357
this.handleKeyDown = function(key, evt) {

0 commit comments

Comments
 (0)