Skip to content

Commit

Permalink
Datepicker: Show four-digit years in title
Browse files Browse the repository at this point in the history
  • Loading branch information
mattheww-skyward committed Jan 22, 2020
1 parent d0b8329 commit f80783d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
13 changes: 9 additions & 4 deletions tests/unit/datepicker/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ QUnit.test( "widget method", function( assert ) {

QUnit.test( "baseStructure", function( assert ) {
var ready = assert.async();
assert.expect( 58 );
assert.expect( 60 );
var header, title, table, thead, week, panel, inl, child,
inp = testHelper.initNewInput(),
inp = testHelper.initNewInput( {
defaultDate: $.datepicker._newDate( 1, 2 - 1, 3 )
} ),
dp = $( "#ui-datepicker-div" );

function step1() {
Expand All @@ -61,7 +63,7 @@ QUnit.test( "baseStructure", function( assert ) {
assert.ok( title.is( "div.ui-datepicker-title" ) && title.html() !== "", "Structure - title division" );
assert.equal( title.children().length, 2, "Structure - title child count" );
assert.ok( title.children().first().is( "span.ui-datepicker-month" ) && title.children().first().text() !== "", "Structure - month text" );
assert.ok( title.children().last().is( "span.ui-datepicker-year" ) && title.children().last().text() !== "", "Structure - year text" );
assert.ok( title.children().last().is( "span.ui-datepicker-year" ) && title.children().last().text() === "0001", "Structure - year text" );

table = dp.children().eq( 1 );
assert.ok( table.is( "table.ui-datepicker-calendar" ), "Structure - month table" );
Expand Down Expand Up @@ -90,12 +92,15 @@ QUnit.test( "baseStructure", function( assert ) {
inp = testHelper.initNewInput( {
changeMonth: true,
changeYear: true,
showButtonPanel: true
showButtonPanel: true,
defaultDate: $.datepicker._newDate( 1, 2 - 1, 3 )
} );
testHelper.onFocus( inp, function() {
title = dp.find( "div.ui-datepicker-title" );
assert.ok( title.children().first().is( "select.ui-datepicker-month" ), "Structure - month selector" );
assert.ok( title.children().last().is( "select.ui-datepicker-year" ), "Structure - year selector" );
assert.equal( title.children().last().children().first().text(), "-9" );
assert.equal( title.children().last().children().last().text(), "0011" );

panel = dp.children().last();
assert.ok( panel.is( "div.ui-datepicker-buttonpane" ), "Structure - button panel division" );
Expand Down
13 changes: 10 additions & 3 deletions ui/widgets/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ $.extend( Datepicker.prototype, {
output += formatName( "M", date.getMonth(), monthNamesShort, monthNames );
break;
case "y":
output += ( "0000" + date.getFullYear() ).slice( lookAhead( "y" ) ? -4 : -2 );
output += lookAhead( "y" ) ? this._formatYear( date.getFullYear() ) : ( "00" + date.getFullYear() ).slice( -2 );
break;
case "@":
output += date.getTime();
Expand Down Expand Up @@ -1878,7 +1878,7 @@ $.extend( Datepicker.prototype, {
if ( !inst.yearshtml ) {
inst.yearshtml = "";
if ( secondary || !changeYear ) {
html += "<span class='ui-datepicker-year'>" + drawYear + "</span>";
html += "<span class='ui-datepicker-year'>" + this._formatYear( drawYear ) + "</span>";
} else {

// determine range of years to display
Expand All @@ -1898,7 +1898,7 @@ $.extend( Datepicker.prototype, {
for ( ; year <= endYear; year++ ) {
inst.yearshtml += "<option value='" + year + "'" +
( year === drawYear ? " selected='selected'" : "" ) +
">" + year + "</option>";
">" + this._formatYear( year ) + "</option>";
}
inst.yearshtml += "</select>";

Expand Down Expand Up @@ -2040,6 +2040,13 @@ $.extend( Datepicker.prototype, {
date.setFullYear( date.getFullYear() - 1900 );
}
return date;
},

/* Add leading zeros to produce an at-least-four-digit year. */
_formatYear: function (year) {
var yearString = "" + year;
return year < 0 ? yearString :
yearString.length < 4 ? ( "0000" + yearString ).slice( -4 ) : yearString;
}
} );

Expand Down

0 comments on commit f80783d

Please sign in to comment.