Skip to content

Commit ea75bd3

Browse files
Datepicker: Add leading zeros when formatting dates < 1000 with "yy"
1 parent 0c860b0 commit ea75bd3

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

tests/unit/datepicker/options.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ QUnit.test( "Ticket #7244: date parser does not fail when too many numbers are p
10661066
} );
10671067

10681068
QUnit.test( "formatDate", function( assert ) {
1069-
assert.expect( 16 );
1069+
assert.expect( 17 );
10701070
testHelper.init( "#inp" );
10711071
var gmtDate, fr, settings;
10721072
assert.equal( $.datepicker.formatDate( "d m y", new Date( 2001, 2 - 1, 3 ) ),
@@ -1090,6 +1090,8 @@ QUnit.test( "formatDate", function( assert ) {
10901090
assert.equal( $.datepicker.formatDate( "'day' d 'of' MM (''DD''), yy",
10911091
new Date( 2001, 2 - 1, 3 ) ), "day 3 of February ('Saturday'), 2001",
10921092
"Format date 'day' d 'of' MM ('DD'), yy" );
1093+
assert.equal( $.datepicker.formatDate( "yy-mm-dd", $.datepicker._newDate( 999, 2 - 1, 3 ) ),
1094+
"0999-02-03", "Format ancient date yy-mm-dd");
10931095
gmtDate = new Date( 2001, 2 - 1, 3 );
10941096
gmtDate.setMinutes( gmtDate.getMinutes() - gmtDate.getTimezoneOffset() );
10951097
assert.equal( $.datepicker.formatDate( "@", gmtDate ), "981158400000", "Format date @" );

ui/widgets/datepicker.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,8 +1409,7 @@ $.extend( Datepicker.prototype, {
14091409
output += formatName( "M", date.getMonth(), monthNamesShort, monthNames );
14101410
break;
14111411
case "y":
1412-
output += ( lookAhead( "y" ) ? date.getFullYear() :
1413-
( date.getFullYear() % 100 < 10 ? "0" : "" ) + date.getFullYear() % 100 );
1412+
output += ( "0000" + date.getFullYear() ).slice( lookAhead( "y" ) ? -4 : -2 );
14141413
break;
14151414
case "@":
14161415
output += date.getTime();
@@ -2023,6 +2022,18 @@ $.extend( Datepicker.prototype, {
20232022
this._daylightSavingAdjust( new Date( year, month, day ) ) ) :
20242023
this._daylightSavingAdjust( new Date( inst.currentYear, inst.currentMonth, inst.currentDay ) ) );
20252024
return this.formatDate( this._get( inst, "dateFormat" ), date, this._getFormatConfig( inst ) );
2025+
},
2026+
2027+
/* Create a Date from a year, month, and day, accounting for years 0-99. */
2028+
_newDate: function( year, month, day ) {
2029+
var date = new Date( year, month, day );
2030+
2031+
// Offset dates in the incorrect range. Blindly calling setFullYear(year) would not handle out-of-range
2032+
// months/days causing the year to shift.
2033+
if ( year >= 0 && year <= 99 ) {
2034+
date.setFullYear( date.getFullYear() - 1900 );
2035+
}
2036+
return date;
20262037
}
20272038
} );
20282039

0 commit comments

Comments
 (0)