Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Datepicker: Use function to construct new Date from year, month, day
  • Loading branch information
MoonE committed Mar 14, 2026
commit d795365d0a3c1319b1c551e6682927fe1d70f1cf
6 changes: 3 additions & 3 deletions tests/unit/datepicker/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ define( [

return $.extend( helper, {
addMonths: function( date, offset ) {
var maxDay = 32 - new Date( date.getFullYear(), date.getMonth() + offset, 32 ).getDate();
var maxDay = 32 - $.datepicker._createDate( date.getFullYear(), date.getMonth() + offset, 32 ).getDate();
date.setDate( Math.min( date.getDate(), maxDay ) );
date.setMonth( date.getMonth() + offset );
return date;
Expand All @@ -19,8 +19,8 @@ return $.extend( helper, {
assert.ok( false, message + " - missing date" );
return;
}
d1 = new Date( d1.getFullYear(), d1.getMonth(), d1.getDate() );
d2 = new Date( d2.getFullYear(), d2.getMonth(), d2.getDate() );
d1 = $.datepicker._createDate( d1.getFullYear(), d1.getMonth(), d1.getDate() );
d2 = $.datepicker._createDate( d2.getFullYear(), d2.getMonth(), d2.getDate() );
assert.equal( d1.toString(), d2.toString(), message );
},

Expand Down
53 changes: 31 additions & 22 deletions ui/widgets/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ $.extend( Datepicker.prototype, {
} while ( true );
}

date = this._daylightSavingAdjust( new Date( year, month - 1, day ) );
date = this._daylightSavingAdjust( this._createDate( year, month - 1, day ) );
if ( date.getFullYear() !== year || date.getMonth() + 1 !== month || date.getDate() !== day ) {
throw "Invalid date"; // E.g. 31/02/00
}
Expand Down Expand Up @@ -1466,7 +1466,7 @@ $.extend( Datepicker.prototype, {
break;
case "o":
output += formatNumber( "o",
Math.round( ( new Date( date.getFullYear(), date.getMonth(), date.getDate() ).getTime() - new Date( date.getFullYear(), 0, 0 ).getTime() ) / 86400000 ), 3 );
Math.round( ( this._createDate( date.getFullYear(), date.getMonth(), date.getDate() ).getTime() - this._createDate( date.getFullYear(), 0, 0 ).getTime() ) / 86400000 ), 3 );
break;
case "m":
output += formatNumber( "m", date.getMonth() + 1, 2 );
Expand Down Expand Up @@ -1625,7 +1625,7 @@ $.extend( Datepicker.prototype, {
}
matches = pattern.exec( offset );
}
return new Date( year, month, day );
return $.datepicker._createDate( year, month, day );
},
newDate = ( date == null || date === "" ? defaultDate : ( typeof date === "string" ? offsetString( date ) :
( typeof date === "number" ? ( isNaN( date ) ? defaultDate : offsetNumeric( date ) ) : new Date( date.getTime() ) ) ) );
Expand Down Expand Up @@ -1677,9 +1677,9 @@ $.extend( Datepicker.prototype, {
/* Retrieve the date(s) directly. */
_getDate: function( inst ) {
var startDate = ( !inst.currentYear || ( inst.input && inst.input.val() === "" ) ? null :
this._daylightSavingAdjust( new Date(
inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
return startDate;
this._daylightSavingAdjust( this._createDate(
inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
return startDate;
},

/* Attach the onxxx handlers. These are declared statically so
Expand Down Expand Up @@ -1729,7 +1729,7 @@ $.extend( Datepicker.prototype, {
printDate, dRow, tbody, daySettings, otherMonth, unselectable,
tempDate = new Date(),
today = this._daylightSavingAdjust(
new Date( tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate() ) ), // clear time
this._createDate( tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate() ) ), // clear time
isRTL = this._get( inst, "isRTL" ),
showButtonPanel = this._get( inst, "showButtonPanel" ),
hideIfNoPrevNext = this._get( inst, "hideIfNoPrevNext" ),
Expand All @@ -1739,7 +1739,7 @@ $.extend( Datepicker.prototype, {
stepMonths = this._get( inst, "stepMonths" ),
isMultiMonth = ( numMonths[ 0 ] !== 1 || numMonths[ 1 ] !== 1 ),
currentDate = this._daylightSavingAdjust( ( !inst.currentDay ? new Date( 9999, 9, 9 ) :
new Date( inst.currentYear, inst.currentMonth, inst.currentDay ) ) ),
this._createDate( inst.currentYear, inst.currentMonth, inst.currentDay ) ) ),
minDate = this._getMinMaxDate( inst, "min" ),
maxDate = this._getMinMaxDate( inst, "max" ),
drawMonth = inst.drawMonth - showCurrentAtPos,
Expand All @@ -1750,10 +1750,12 @@ $.extend( Datepicker.prototype, {
drawYear--;
}
if ( maxDate ) {
maxDraw = this._daylightSavingAdjust( new Date( maxDate.getFullYear(),
maxDate.getMonth() - ( numMonths[ 0 ] * numMonths[ 1 ] ) + 1, maxDate.getDate() ) );
maxDraw = this._daylightSavingAdjust( this._createDate(
maxDate.getFullYear(),
maxDate.getMonth() - ( numMonths[ 0 ] * numMonths[ 1 ] ) + 1,
maxDate.getDate() ) );
maxDraw = ( minDate && maxDraw < minDate ? minDate : maxDraw );
while ( this._daylightSavingAdjust( new Date( drawYear, drawMonth, 1 ) ) > maxDraw ) {
while ( this._daylightSavingAdjust( this._createDate( drawYear, drawMonth, 1 ) ) > maxDraw ) {
drawMonth--;
if ( drawMonth < 0 ) {
drawMonth = 11;
Expand All @@ -1766,7 +1768,7 @@ $.extend( Datepicker.prototype, {

prevText = this._get( inst, "prevText" );
prevText = ( !navigationAsDateFormat ? prevText : this.formatDate( prevText,
this._daylightSavingAdjust( new Date( drawYear, drawMonth - stepMonths, 1 ) ),
this._daylightSavingAdjust( this._createDate( drawYear, drawMonth - stepMonths, 1 ) ),
this._getFormatConfig( inst ) ) );

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

nextText = this._get( inst, "nextText" );
nextText = ( !navigationAsDateFormat ? nextText : this.formatDate( nextText,
this._daylightSavingAdjust( new Date( drawYear, drawMonth + stepMonths, 1 ) ),
this._daylightSavingAdjust( this._createDate( drawYear, drawMonth + stepMonths, 1 ) ),
this._getFormatConfig( inst ) ) );

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

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

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

/* Find the day of the week of the first of a month. */
_getFirstDayOfMonth: function( year, month ) {
return new Date( year, month, 1 ).getDay();
return this._createDate( year, month, 1 ).getDay();
},

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

if ( offset < 0 ) {
date.setDate( this._getDaysInMonth( date.getFullYear(), date.getMonth() ) );
Expand Down Expand Up @@ -2163,9 +2167,14 @@ $.extend( Datepicker.prototype, {
inst.currentYear = inst.selectedYear;
}
var date = ( day ? ( typeof day === "object" ? day :
this._daylightSavingAdjust( new Date( year, month, day ) ) ) :
this._daylightSavingAdjust( new Date( inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
this._daylightSavingAdjust( this._createDate( year, month, day ) ) ) :
this._daylightSavingAdjust( this._createDate( inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
return this.formatDate( this._get( inst, "dateFormat" ), date, this._getFormatConfig( inst ) );
},

/** Create a date object */
_createDate: function( year, month, day ) {
return new Date( year, month, day );
}
} );

Expand Down