Skip to content

Commit d0b8329

Browse files
Datepicker: Handle dates before year 100
Fixes #7098
1 parent 862121e commit d0b8329

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

Diff for: tests/unit/datepicker/core.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ QUnit.test( "keystrokes", function( assert ) {
457457
} );
458458

459459
QUnit.test( "mouse", function( assert ) {
460-
assert.expect( 15 );
460+
assert.expect( 16 );
461461
var inl,
462462
inp = testHelper.init( "#inp" ),
463463
dp = $( "#ui-datepicker-div" ),
@@ -470,6 +470,10 @@ QUnit.test( "mouse", function( assert ) {
470470
$( ".ui-datepicker-calendar tbody a:contains(12)", dp ).simulate( "click", {} );
471471
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), new Date( 2008, 2 - 1, 12 ),
472472
"Mouse click - preset" );
473+
inp.val( "02/04/0001" ).datepicker( "show" );
474+
$( ".ui-datepicker-calendar tbody a:contains(12)", dp ).simulate( "click", {} );
475+
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), $.datepicker._newDate( 1, 2 - 1, 12 ),
476+
"Mouse click - year 0-99" );
473477
inp.val( "02/04/2008" ).datepicker( "show" );
474478
inp.val( "" ).datepicker( "show" );
475479
$( "button.ui-datepicker-close", dp ).simulate( "click", {} );

Diff for: tests/unit/datepicker/options.js

+20-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ QUnit.test( "otherMonths", function( assert ) {
267267
} );
268268

269269
QUnit.test( "defaultDate", function( assert ) {
270-
assert.expect( 16 );
270+
assert.expect( 18 );
271271
var inp = testHelper.init( "#inp" ),
272272
date = new Date();
273273
inp.val( "" ).datepicker( "show" ).
@@ -350,6 +350,11 @@ QUnit.test( "defaultDate", function( assert ) {
350350
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
351351
date = new Date( 2007, 7 - 1, 4 );
352352
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date, "Default date 07/04/2007" );
353+
inp.datepicker( "option", { defaultDate: "07/04/0001" } ).
354+
datepicker( "hide" ).val( "" ).datepicker( "show" ).
355+
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
356+
date = $.datepicker._newDate( 1, 7 - 1, 4 );
357+
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date, "Default date 07/04/0001" );
353358
inp.datepicker( "option", { dateFormat: "yy-mm-dd", defaultDate: "2007-04-02" } ).
354359
datepicker( "hide" ).val( "" ).datepicker( "show" ).
355360
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
@@ -362,6 +367,11 @@ QUnit.test( "defaultDate", function( assert ) {
362367
datepicker( "hide" ).val( "" ).datepicker( "show" ).
363368
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
364369
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date, "Default date 01/26/2007" );
370+
date = $.datepicker._newDate( 1, 1 - 1, 26 );
371+
inp.datepicker( "option", { defaultDate: date } ).
372+
datepicker( "hide" ).val( "" ).datepicker( "show" ).
373+
simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
374+
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date, "Default date 01/26/0001" );
365375
} );
366376

367377
QUnit.test( "miscellaneous", function( assert ) {
@@ -537,14 +547,17 @@ QUnit.test( "minMax", function( assert ) {
537547
} );
538548

539549
QUnit.test( "setDate", function( assert ) {
540-
assert.expect( 24 );
550+
assert.expect( 26 );
541551
var inl, alt, minDate, maxDate, dateAndTimeToSet, dateAndTimeClone,
542552
inp = testHelper.init( "#inp" ),
543553
date1 = new Date( 2008, 6 - 1, 4 ),
544-
date2 = new Date();
554+
date2 = new Date(),
555+
date3 = $.datepicker._newDate( 1, 4 - 1, 1 );
545556
assert.ok( inp.datepicker( "getDate" ) == null, "Set date - default" );
546557
inp.datepicker( "setDate", date1 );
547558
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date1, "Set date - 2008-06-04" );
559+
inp.datepicker( "setDate", date3 );
560+
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date3, "Set date - 0001-04-01" );
548561
date1 = new Date();
549562
date1.setDate( date1.getDate() + 7 );
550563
inp.datepicker( "setDate", +7 );
@@ -568,6 +581,10 @@ QUnit.test( "setDate", function( assert ) {
568581
date1.setDate( date1.getDate() - 21 );
569582
inp.datepicker( "setDate", "c -3 w" );
570583
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date1, "Set date - c -3 w" );
584+
date3 = new Date(date1);
585+
date3.setFullYear( 1 );
586+
inp.datepicker( "setDate", "c " + (1 - date1.getFullYear()) + " y" );
587+
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date3, "Set date - 0001 relatively" );
571588

572589
// Inline
573590
inl = testHelper.init( "#inl" );

Diff for: ui/widgets/datepicker.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ $.extend( Datepicker.prototype, {
14061406
break;
14071407
case "o":
14081408
output += formatNumber( "o",
1409-
Math.round( ( new Date( date.getFullYear(), date.getMonth(), date.getDate() ).getTime() - new Date( date.getFullYear(), 0, 0 ).getTime() ) / 86400000 ), 3 );
1409+
Math.round( ( this._newDate( date.getFullYear(), date.getMonth(), date.getDate() ).getTime() - this._newDate( date.getFullYear(), 0, 0 ).getTime() ) / 86400000 ), 3 );
14101410
break;
14111411
case "m":
14121412
output += formatNumber( "m", date.getMonth() + 1, 2 );
@@ -1563,7 +1563,7 @@ $.extend( Datepicker.prototype, {
15631563
}
15641564
matches = pattern.exec( offset );
15651565
}
1566-
return new Date( year, month, day );
1566+
return $.datepicker._newDate( year, month, day );
15671567
},
15681568
newDate = ( date == null || date === "" ? defaultDate : ( typeof date === "string" ? offsetString( date ) :
15691569
( typeof date === "number" ? ( isNaN( date ) ? defaultDate : offsetNumeric( date ) ) : new Date( date.getTime() ) ) ) );
@@ -1615,7 +1615,7 @@ $.extend( Datepicker.prototype, {
16151615
/* Retrieve the date(s) directly. */
16161616
_getDate: function( inst ) {
16171617
var startDate = ( !inst.currentYear || ( inst.input && inst.input.val() === "" ) ? null :
1618-
this._daylightSavingAdjust( new Date(
1618+
this._daylightSavingAdjust( this._newDate(
16191619
inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
16201620
return startDate;
16211621
},
@@ -1667,7 +1667,7 @@ $.extend( Datepicker.prototype, {
16671667
printDate, dRow, tbody, daySettings, otherMonth, unselectable,
16681668
tempDate = new Date(),
16691669
today = this._daylightSavingAdjust(
1670-
new Date( tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate() ) ), // clear time
1670+
this._newDate( tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate() ) ), // clear time
16711671
isRTL = this._get( inst, "isRTL" ),
16721672
showButtonPanel = this._get( inst, "showButtonPanel" ),
16731673
hideIfNoPrevNext = this._get( inst, "hideIfNoPrevNext" ),
@@ -1677,7 +1677,7 @@ $.extend( Datepicker.prototype, {
16771677
stepMonths = this._get( inst, "stepMonths" ),
16781678
isMultiMonth = ( numMonths[ 0 ] !== 1 || numMonths[ 1 ] !== 1 ),
16791679
currentDate = this._daylightSavingAdjust( ( !inst.currentDay ? new Date( 9999, 9, 9 ) :
1680-
new Date( inst.currentYear, inst.currentMonth, inst.currentDay ) ) ),
1680+
this._newDate( inst.currentYear, inst.currentMonth, inst.currentDay ) ) ),
16811681
minDate = this._getMinMaxDate( inst, "min" ),
16821682
maxDate = this._getMinMaxDate( inst, "max" ),
16831683
drawMonth = inst.drawMonth - showCurrentAtPos,
@@ -1688,10 +1688,10 @@ $.extend( Datepicker.prototype, {
16881688
drawYear--;
16891689
}
16901690
if ( maxDate ) {
1691-
maxDraw = this._daylightSavingAdjust( new Date( maxDate.getFullYear(),
1691+
maxDraw = this._daylightSavingAdjust( this._newDate( maxDate.getFullYear(),
16921692
maxDate.getMonth() - ( numMonths[ 0 ] * numMonths[ 1 ] ) + 1, maxDate.getDate() ) );
16931693
maxDraw = ( minDate && maxDraw < minDate ? minDate : maxDraw );
1694-
while ( this._daylightSavingAdjust( new Date( drawYear, drawMonth, 1 ) ) > maxDraw ) {
1694+
while ( this._daylightSavingAdjust( this._newDate( drawYear, drawMonth, 1 ) ) > maxDraw ) {
16951695
drawMonth--;
16961696
if ( drawMonth < 0 ) {
16971697
drawMonth = 11;
@@ -1704,7 +1704,7 @@ $.extend( Datepicker.prototype, {
17041704

17051705
prevText = this._get( inst, "prevText" );
17061706
prevText = ( !navigationAsDateFormat ? prevText : this.formatDate( prevText,
1707-
this._daylightSavingAdjust( new Date( drawYear, drawMonth - stepMonths, 1 ) ),
1707+
this._daylightSavingAdjust( this._newDate( drawYear, drawMonth - stepMonths, 1 ) ),
17081708
this._getFormatConfig( inst ) ) );
17091709

17101710
prev = ( this._canAdjustMonth( inst, -1, drawYear, drawMonth ) ?
@@ -1714,7 +1714,7 @@ $.extend( Datepicker.prototype, {
17141714

17151715
nextText = this._get( inst, "nextText" );
17161716
nextText = ( !navigationAsDateFormat ? nextText : this.formatDate( nextText,
1717-
this._daylightSavingAdjust( new Date( drawYear, drawMonth + stepMonths, 1 ) ),
1717+
this._daylightSavingAdjust( this._newDate( drawYear, drawMonth + stepMonths, 1 ) ),
17181718
this._getFormatConfig( inst ) ) );
17191719

17201720
next = ( this._canAdjustMonth( inst, +1, drawYear, drawMonth ) ?
@@ -1752,7 +1752,7 @@ $.extend( Datepicker.prototype, {
17521752
group = "";
17531753
this.maxRows = 4;
17541754
for ( col = 0; col < numMonths[ 1 ]; col++ ) {
1755-
selectedDate = this._daylightSavingAdjust( new Date( drawYear, drawMonth, inst.selectedDay ) );
1755+
selectedDate = this._daylightSavingAdjust( this._newDate( drawYear, drawMonth, inst.selectedDay ) );
17561756
cornerClass = " ui-corner-all";
17571757
calender = "";
17581758
if ( isMultiMonth ) {
@@ -1790,7 +1790,7 @@ $.extend( Datepicker.prototype, {
17901790
curRows = Math.ceil( ( leadDays + daysInMonth ) / 7 ); // calculate the number of rows to generate
17911791
numRows = ( isMultiMonth ? this.maxRows > curRows ? this.maxRows : curRows : curRows ); //If multiple months, use the higher number of rows (see #7043)
17921792
this.maxRows = numRows;
1793-
printDate = this._daylightSavingAdjust( new Date( drawYear, drawMonth, 1 - leadDays ) );
1793+
printDate = this._daylightSavingAdjust( this._newDate( drawYear, drawMonth, 1 - leadDays ) );
17941794
for ( dRow = 0; dRow < numRows; dRow++ ) { // create date picker rows
17951795
calender += "<tr>";
17961796
tbody = ( !showWeek ? "" : "<td class='ui-datepicker-week-col'>" +
@@ -1920,7 +1920,7 @@ $.extend( Datepicker.prototype, {
19201920
var year = inst.selectedYear + ( period === "Y" ? offset : 0 ),
19211921
month = inst.selectedMonth + ( period === "M" ? offset : 0 ),
19221922
day = Math.min( inst.selectedDay, this._getDaysInMonth( year, month ) ) + ( period === "D" ? offset : 0 ),
1923-
date = this._restrictMinMax( inst, this._daylightSavingAdjust( new Date( year, month, day ) ) );
1923+
date = this._restrictMinMax( inst, this._daylightSavingAdjust( this._newDate( year, month, day ) ) );
19241924

19251925
inst.selectedDay = date.getDate();
19261926
inst.drawMonth = inst.selectedMonth = date.getMonth();
@@ -1960,18 +1960,18 @@ $.extend( Datepicker.prototype, {
19601960

19611961
/* Find the number of days in a given month. */
19621962
_getDaysInMonth: function( year, month ) {
1963-
return 32 - this._daylightSavingAdjust( new Date( year, month, 32 ) ).getDate();
1963+
return 32 - this._daylightSavingAdjust( this._newDate( year, month, 32 ) ).getDate();
19641964
},
19651965

19661966
/* Find the day of the week of the first of a month. */
19671967
_getFirstDayOfMonth: function( year, month ) {
1968-
return new Date( year, month, 1 ).getDay();
1968+
return this._newDate( year, month, 1 ).getDay();
19691969
},
19701970

19711971
/* Determines if we should allow a "next/prev" month display change. */
19721972
_canAdjustMonth: function( inst, offset, curYear, curMonth ) {
19731973
var numMonths = this._getNumberOfMonths( inst ),
1974-
date = this._daylightSavingAdjust( new Date( curYear,
1974+
date = this._daylightSavingAdjust( this._newDate( curYear,
19751975
curMonth + ( offset < 0 ? offset : numMonths[ 0 ] * numMonths[ 1 ] ), 1 ) );
19761976

19771977
if ( offset < 0 ) {
@@ -2025,8 +2025,8 @@ $.extend( Datepicker.prototype, {
20252025
inst.currentYear = inst.selectedYear;
20262026
}
20272027
var date = ( day ? ( typeof day === "object" ? day :
2028-
this._daylightSavingAdjust( new Date( year, month, day ) ) ) :
2029-
this._daylightSavingAdjust( new Date( inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
2028+
this._daylightSavingAdjust( this._newDate( year, month, day ) ) ) :
2029+
this._daylightSavingAdjust( this._newDate( inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
20302030
return this.formatDate( this._get( inst, "dateFormat" ), date, this._getFormatConfig( inst ) );
20312031
},
20322032

0 commit comments

Comments
 (0)