Skip to content

Commit d795365

Browse files
committed
Datepicker: Use function to construct new Date from year, month, day
1 parent f5ee24c commit d795365

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

tests/unit/datepicker/helper.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ define( [
88

99
return $.extend( helper, {
1010
addMonths: function( date, offset ) {
11-
var maxDay = 32 - new Date( date.getFullYear(), date.getMonth() + offset, 32 ).getDate();
11+
var maxDay = 32 - $.datepicker._createDate( date.getFullYear(), date.getMonth() + offset, 32 ).getDate();
1212
date.setDate( Math.min( date.getDate(), maxDay ) );
1313
date.setMonth( date.getMonth() + offset );
1414
return date;
@@ -19,8 +19,8 @@ return $.extend( helper, {
1919
assert.ok( false, message + " - missing date" );
2020
return;
2121
}
22-
d1 = new Date( d1.getFullYear(), d1.getMonth(), d1.getDate() );
23-
d2 = new Date( d2.getFullYear(), d2.getMonth(), d2.getDate() );
22+
d1 = $.datepicker._createDate( d1.getFullYear(), d1.getMonth(), d1.getDate() );
23+
d2 = $.datepicker._createDate( d2.getFullYear(), d2.getMonth(), d2.getDate() );
2424
assert.equal( d1.toString(), d2.toString(), message );
2525
},
2626

ui/widgets/datepicker.js

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ $.extend( Datepicker.prototype, {
13571357
} while ( true );
13581358
}
13591359

1360-
date = this._daylightSavingAdjust( new Date( year, month - 1, day ) );
1360+
date = this._daylightSavingAdjust( this._createDate( year, month - 1, day ) );
13611361
if ( date.getFullYear() !== year || date.getMonth() + 1 !== month || date.getDate() !== day ) {
13621362
throw "Invalid date"; // E.g. 31/02/00
13631363
}
@@ -1466,7 +1466,7 @@ $.extend( Datepicker.prototype, {
14661466
break;
14671467
case "o":
14681468
output += formatNumber( "o",
1469-
Math.round( ( new Date( date.getFullYear(), date.getMonth(), date.getDate() ).getTime() - new Date( date.getFullYear(), 0, 0 ).getTime() ) / 86400000 ), 3 );
1469+
Math.round( ( this._createDate( date.getFullYear(), date.getMonth(), date.getDate() ).getTime() - this._createDate( date.getFullYear(), 0, 0 ).getTime() ) / 86400000 ), 3 );
14701470
break;
14711471
case "m":
14721472
output += formatNumber( "m", date.getMonth() + 1, 2 );
@@ -1625,7 +1625,7 @@ $.extend( Datepicker.prototype, {
16251625
}
16261626
matches = pattern.exec( offset );
16271627
}
1628-
return new Date( year, month, day );
1628+
return $.datepicker._createDate( year, month, day );
16291629
},
16301630
newDate = ( date == null || date === "" ? defaultDate : ( typeof date === "string" ? offsetString( date ) :
16311631
( typeof date === "number" ? ( isNaN( date ) ? defaultDate : offsetNumeric( date ) ) : new Date( date.getTime() ) ) ) );
@@ -1677,9 +1677,9 @@ $.extend( Datepicker.prototype, {
16771677
/* Retrieve the date(s) directly. */
16781678
_getDate: function( inst ) {
16791679
var startDate = ( !inst.currentYear || ( inst.input && inst.input.val() === "" ) ? null :
1680-
this._daylightSavingAdjust( new Date(
1681-
inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
1682-
return startDate;
1680+
this._daylightSavingAdjust( this._createDate(
1681+
inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
1682+
return startDate;
16831683
},
16841684

16851685
/* Attach the onxxx handlers. These are declared statically so
@@ -1729,7 +1729,7 @@ $.extend( Datepicker.prototype, {
17291729
printDate, dRow, tbody, daySettings, otherMonth, unselectable,
17301730
tempDate = new Date(),
17311731
today = this._daylightSavingAdjust(
1732-
new Date( tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate() ) ), // clear time
1732+
this._createDate( tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate() ) ), // clear time
17331733
isRTL = this._get( inst, "isRTL" ),
17341734
showButtonPanel = this._get( inst, "showButtonPanel" ),
17351735
hideIfNoPrevNext = this._get( inst, "hideIfNoPrevNext" ),
@@ -1739,7 +1739,7 @@ $.extend( Datepicker.prototype, {
17391739
stepMonths = this._get( inst, "stepMonths" ),
17401740
isMultiMonth = ( numMonths[ 0 ] !== 1 || numMonths[ 1 ] !== 1 ),
17411741
currentDate = this._daylightSavingAdjust( ( !inst.currentDay ? new Date( 9999, 9, 9 ) :
1742-
new Date( inst.currentYear, inst.currentMonth, inst.currentDay ) ) ),
1742+
this._createDate( inst.currentYear, inst.currentMonth, inst.currentDay ) ) ),
17431743
minDate = this._getMinMaxDate( inst, "min" ),
17441744
maxDate = this._getMinMaxDate( inst, "max" ),
17451745
drawMonth = inst.drawMonth - showCurrentAtPos,
@@ -1750,10 +1750,12 @@ $.extend( Datepicker.prototype, {
17501750
drawYear--;
17511751
}
17521752
if ( maxDate ) {
1753-
maxDraw = this._daylightSavingAdjust( new Date( maxDate.getFullYear(),
1754-
maxDate.getMonth() - ( numMonths[ 0 ] * numMonths[ 1 ] ) + 1, maxDate.getDate() ) );
1753+
maxDraw = this._daylightSavingAdjust( this._createDate(
1754+
maxDate.getFullYear(),
1755+
maxDate.getMonth() - ( numMonths[ 0 ] * numMonths[ 1 ] ) + 1,
1756+
maxDate.getDate() ) );
17551757
maxDraw = ( minDate && maxDraw < minDate ? minDate : maxDraw );
1756-
while ( this._daylightSavingAdjust( new Date( drawYear, drawMonth, 1 ) ) > maxDraw ) {
1758+
while ( this._daylightSavingAdjust( this._createDate( drawYear, drawMonth, 1 ) ) > maxDraw ) {
17571759
drawMonth--;
17581760
if ( drawMonth < 0 ) {
17591761
drawMonth = 11;
@@ -1766,7 +1768,7 @@ $.extend( Datepicker.prototype, {
17661768

17671769
prevText = this._get( inst, "prevText" );
17681770
prevText = ( !navigationAsDateFormat ? prevText : this.formatDate( prevText,
1769-
this._daylightSavingAdjust( new Date( drawYear, drawMonth - stepMonths, 1 ) ),
1771+
this._daylightSavingAdjust( this._createDate( drawYear, drawMonth - stepMonths, 1 ) ),
17701772
this._getFormatConfig( inst ) ) );
17711773

17721774
if ( this._canAdjustMonth( inst, -1, drawYear, drawMonth ) ) {
@@ -1801,7 +1803,7 @@ $.extend( Datepicker.prototype, {
18011803

18021804
nextText = this._get( inst, "nextText" );
18031805
nextText = ( !navigationAsDateFormat ? nextText : this.formatDate( nextText,
1804-
this._daylightSavingAdjust( new Date( drawYear, drawMonth + stepMonths, 1 ) ),
1806+
this._daylightSavingAdjust( this._createDate( drawYear, drawMonth + stepMonths, 1 ) ),
18051807
this._getFormatConfig( inst ) ) );
18061808

18071809
if ( this._canAdjustMonth( inst, +1, drawYear, drawMonth ) ) {
@@ -1886,7 +1888,7 @@ $.extend( Datepicker.prototype, {
18861888
group = "";
18871889
this.maxRows = 4;
18881890
for ( col = 0; col < numMonths[ 1 ]; col++ ) {
1889-
selectedDate = this._daylightSavingAdjust( new Date( drawYear, drawMonth, inst.selectedDay ) );
1891+
selectedDate = this._daylightSavingAdjust( this._createDate( drawYear, drawMonth, inst.selectedDay ) );
18901892
cornerClass = " ui-corner-all";
18911893
calender = "";
18921894
if ( isMultiMonth ) {
@@ -1924,7 +1926,7 @@ $.extend( Datepicker.prototype, {
19241926
curRows = Math.ceil( ( leadDays + daysInMonth ) / 7 ); // calculate the number of rows to generate
19251927
numRows = ( isMultiMonth ? this.maxRows > curRows ? this.maxRows : curRows : curRows ); //If multiple months, use the higher number of rows (see #7043)
19261928
this.maxRows = numRows;
1927-
printDate = this._daylightSavingAdjust( new Date( drawYear, drawMonth, 1 - leadDays ) );
1929+
printDate = this._daylightSavingAdjust( this._createDate( drawYear, drawMonth, 1 - leadDays ) );
19281930
for ( dRow = 0; dRow < numRows; dRow++ ) { // create date picker rows
19291931
calender += "<tr>";
19301932
tbody = ( !showWeek ? "" : "<td class='ui-datepicker-week-col'>" +
@@ -2058,7 +2060,7 @@ $.extend( Datepicker.prototype, {
20582060
var year = inst.selectedYear + ( period === "Y" ? offset : 0 ),
20592061
month = inst.selectedMonth + ( period === "M" ? offset : 0 ),
20602062
day = Math.min( inst.selectedDay, this._getDaysInMonth( year, month ) ) + ( period === "D" ? offset : 0 ),
2061-
date = this._restrictMinMax( inst, this._daylightSavingAdjust( new Date( year, month, day ) ) );
2063+
date = this._restrictMinMax( inst, this._daylightSavingAdjust( this._createDate( year, month, day ) ) );
20622064

20632065
inst.selectedDay = date.getDate();
20642066
inst.drawMonth = inst.selectedMonth = date.getMonth();
@@ -2098,19 +2100,21 @@ $.extend( Datepicker.prototype, {
20982100

20992101
/* Find the number of days in a given month. */
21002102
_getDaysInMonth: function( year, month ) {
2101-
return 32 - this._daylightSavingAdjust( new Date( year, month, 32 ) ).getDate();
2103+
return 32 - this._daylightSavingAdjust( this._createDate( year, month, 32 ) ).getDate();
21022104
},
21032105

21042106
/* Find the day of the week of the first of a month. */
21052107
_getFirstDayOfMonth: function( year, month ) {
2106-
return new Date( year, month, 1 ).getDay();
2108+
return this._createDate( year, month, 1 ).getDay();
21072109
},
21082110

21092111
/* Determines if we should allow a "next/prev" month display change. */
21102112
_canAdjustMonth: function( inst, offset, curYear, curMonth ) {
21112113
var numMonths = this._getNumberOfMonths( inst ),
2112-
date = this._daylightSavingAdjust( new Date( curYear,
2113-
curMonth + ( offset < 0 ? offset : numMonths[ 0 ] * numMonths[ 1 ] ), 1 ) );
2114+
date = this._daylightSavingAdjust( this._createDate(
2115+
curYear,
2116+
curMonth + ( offset < 0 ? offset : numMonths[ 0 ] * numMonths[ 1 ] ),
2117+
1 ) );
21142118

21152119
if ( offset < 0 ) {
21162120
date.setDate( this._getDaysInMonth( date.getFullYear(), date.getMonth() ) );
@@ -2163,9 +2167,14 @@ $.extend( Datepicker.prototype, {
21632167
inst.currentYear = inst.selectedYear;
21642168
}
21652169
var date = ( day ? ( typeof day === "object" ? day :
2166-
this._daylightSavingAdjust( new Date( year, month, day ) ) ) :
2167-
this._daylightSavingAdjust( new Date( inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
2170+
this._daylightSavingAdjust( this._createDate( year, month, day ) ) ) :
2171+
this._daylightSavingAdjust( this._createDate( inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
21682172
return this.formatDate( this._get( inst, "dateFormat" ), date, this._getFormatConfig( inst ) );
2173+
},
2174+
2175+
/** Create a date object */
2176+
_createDate: function( year, month, day ) {
2177+
return new Date( year, month, day );
21692178
}
21702179
} );
21712180

0 commit comments

Comments
 (0)