Skip to content

Commit c319675

Browse files
committed
Datepicker: Use function to construct new Date from year, month, day
1 parent 997904e commit c319675

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
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: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ $.extend( Datepicker.prototype, {
327327
_autoSize: function( inst ) {
328328
if ( this._get( inst, "autoSize" ) && !inst.inline ) {
329329
var findMax, max, maxI, i,
330-
date = new Date( 2009, 12 - 1, 20 ), // Ensure double digits
330+
date = this._createDate( 2009, 12 - 1, 20 ), // Ensure double digits
331331
dateFormat = this._get( inst, "dateFormat" );
332332

333333
if ( dateFormat.match( /[DM]/ ) ) {
@@ -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" ),
@@ -1738,8 +1738,8 @@ $.extend( Datepicker.prototype, {
17381738
showCurrentAtPos = this._get( inst, "showCurrentAtPos" ),
17391739
stepMonths = this._get( inst, "stepMonths" ),
17401740
isMultiMonth = ( numMonths[ 0 ] !== 1 || numMonths[ 1 ] !== 1 ),
1741-
currentDate = this._daylightSavingAdjust( ( !inst.currentDay ? new Date( 9999, 9, 9 ) :
1742-
new Date( inst.currentYear, inst.currentMonth, inst.currentDay ) ) ),
1741+
currentDate = this._daylightSavingAdjust( ( !inst.currentDay ? this._createDate( 9999, 9, 9 ) :
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,13 @@ $.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()
1757+
) );
17551758
maxDraw = ( minDate && maxDraw < minDate ? minDate : maxDraw );
1756-
while ( this._daylightSavingAdjust( new Date( drawYear, drawMonth, 1 ) ) > maxDraw ) {
1759+
while ( this._daylightSavingAdjust( this._createDate( drawYear, drawMonth, 1 ) ) > maxDraw ) {
17571760
drawMonth--;
17581761
if ( drawMonth < 0 ) {
17591762
drawMonth = 11;
@@ -1766,7 +1769,7 @@ $.extend( Datepicker.prototype, {
17661769

17671770
prevText = this._get( inst, "prevText" );
17681771
prevText = ( !navigationAsDateFormat ? prevText : this.formatDate( prevText,
1769-
this._daylightSavingAdjust( new Date( drawYear, drawMonth - stepMonths, 1 ) ),
1772+
this._daylightSavingAdjust( this._createDate( drawYear, drawMonth - stepMonths, 1 ) ),
17701773
this._getFormatConfig( inst ) ) );
17711774

17721775
if ( this._canAdjustMonth( inst, -1, drawYear, drawMonth ) ) {
@@ -1801,7 +1804,7 @@ $.extend( Datepicker.prototype, {
18011804

18021805
nextText = this._get( inst, "nextText" );
18031806
nextText = ( !navigationAsDateFormat ? nextText : this.formatDate( nextText,
1804-
this._daylightSavingAdjust( new Date( drawYear, drawMonth + stepMonths, 1 ) ),
1807+
this._daylightSavingAdjust( this._createDate( drawYear, drawMonth + stepMonths, 1 ) ),
18051808
this._getFormatConfig( inst ) ) );
18061809

18071810
if ( this._canAdjustMonth( inst, +1, drawYear, drawMonth ) ) {
@@ -1886,7 +1889,7 @@ $.extend( Datepicker.prototype, {
18861889
group = "";
18871890
this.maxRows = 4;
18881891
for ( col = 0; col < numMonths[ 1 ]; col++ ) {
1889-
selectedDate = this._daylightSavingAdjust( new Date( drawYear, drawMonth, inst.selectedDay ) );
1892+
selectedDate = this._daylightSavingAdjust( this._createDate( drawYear, drawMonth, inst.selectedDay ) );
18901893
cornerClass = " ui-corner-all";
18911894
calender = "";
18921895
if ( isMultiMonth ) {
@@ -1924,7 +1927,7 @@ $.extend( Datepicker.prototype, {
19241927
curRows = Math.ceil( ( leadDays + daysInMonth ) / 7 ); // calculate the number of rows to generate
19251928
numRows = ( isMultiMonth ? this.maxRows > curRows ? this.maxRows : curRows : curRows ); //If multiple months, use the higher number of rows (see #7043)
19261929
this.maxRows = numRows;
1927-
printDate = this._daylightSavingAdjust( new Date( drawYear, drawMonth, 1 - leadDays ) );
1930+
printDate = this._daylightSavingAdjust( this._createDate( drawYear, drawMonth, 1 - leadDays ) );
19281931
for ( dRow = 0; dRow < numRows; dRow++ ) { // create date picker rows
19291932
calender += "<tr>";
19301933
tbody = ( !showWeek ? "" : "<td class='ui-datepicker-week-col'>" +
@@ -2058,7 +2061,7 @@ $.extend( Datepicker.prototype, {
20582061
var year = inst.selectedYear + ( period === "Y" ? offset : 0 ),
20592062
month = inst.selectedMonth + ( period === "M" ? offset : 0 ),
20602063
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 ) ) );
2064+
date = this._restrictMinMax( inst, this._daylightSavingAdjust( this._createDate( year, month, day ) ) );
20622065

20632066
inst.selectedDay = date.getDate();
20642067
inst.drawMonth = inst.selectedMonth = date.getMonth();
@@ -2098,19 +2101,22 @@ $.extend( Datepicker.prototype, {
20982101

20992102
/* Find the number of days in a given month. */
21002103
_getDaysInMonth: function( year, month ) {
2101-
return 32 - this._daylightSavingAdjust( new Date( year, month, 32 ) ).getDate();
2104+
return ( 32 - this._daylightSavingAdjust( this._createDate( year, month, 32 ) ).getDate() );
21022105
},
21032106

21042107
/* Find the day of the week of the first of a month. */
21052108
_getFirstDayOfMonth: function( year, month ) {
2106-
return new Date( year, month, 1 ).getDay();
2109+
return this._createDate( year, month, 1 ).getDay();
21072110
},
21082111

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

21152121
if ( offset < 0 ) {
21162122
date.setDate( this._getDaysInMonth( date.getFullYear(), date.getMonth() ) );
@@ -2163,9 +2169,14 @@ $.extend( Datepicker.prototype, {
21632169
inst.currentYear = inst.selectedYear;
21642170
}
21652171
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 ) ) );
2172+
this._daylightSavingAdjust( this._createDate( year, month, day ) ) ) :
2173+
this._daylightSavingAdjust( this._createDate( inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
21682174
return this.formatDate( this._get( inst, "dateFormat" ), date, this._getFormatConfig( inst ) );
2175+
},
2176+
2177+
/** Create a date object */
2178+
_createDate: function( year, month, day ) {
2179+
return new Date( year, month, day );
21692180
}
21702181
} );
21712182

0 commit comments

Comments
 (0)