Skip to content

Commit f80783d

Browse files
Datepicker: Show four-digit years in title
1 parent d0b8329 commit f80783d

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

tests/unit/datepicker/core.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ QUnit.test( "widget method", function( assert ) {
3939

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

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

6668
table = dp.children().eq( 1 );
6769
assert.ok( table.is( "table.ui-datepicker-calendar" ), "Structure - month table" );
@@ -90,12 +92,15 @@ QUnit.test( "baseStructure", function( assert ) {
9092
inp = testHelper.initNewInput( {
9193
changeMonth: true,
9294
changeYear: true,
93-
showButtonPanel: true
95+
showButtonPanel: true,
96+
defaultDate: $.datepicker._newDate( 1, 2 - 1, 3 )
9497
} );
9598
testHelper.onFocus( inp, function() {
9699
title = dp.find( "div.ui-datepicker-title" );
97100
assert.ok( title.children().first().is( "select.ui-datepicker-month" ), "Structure - month selector" );
98101
assert.ok( title.children().last().is( "select.ui-datepicker-year" ), "Structure - year selector" );
102+
assert.equal( title.children().last().children().first().text(), "-9" );
103+
assert.equal( title.children().last().children().last().text(), "0011" );
99104

100105
panel = dp.children().last();
101106
assert.ok( panel.is( "div.ui-datepicker-buttonpane" ), "Structure - button panel division" );

ui/widgets/datepicker.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ $.extend( Datepicker.prototype, {
14151415
output += formatName( "M", date.getMonth(), monthNamesShort, monthNames );
14161416
break;
14171417
case "y":
1418-
output += ( "0000" + date.getFullYear() ).slice( lookAhead( "y" ) ? -4 : -2 );
1418+
output += lookAhead( "y" ) ? this._formatYear( date.getFullYear() ) : ( "00" + date.getFullYear() ).slice( -2 );
14191419
break;
14201420
case "@":
14211421
output += date.getTime();
@@ -1878,7 +1878,7 @@ $.extend( Datepicker.prototype, {
18781878
if ( !inst.yearshtml ) {
18791879
inst.yearshtml = "";
18801880
if ( secondary || !changeYear ) {
1881-
html += "<span class='ui-datepicker-year'>" + drawYear + "</span>";
1881+
html += "<span class='ui-datepicker-year'>" + this._formatYear( drawYear ) + "</span>";
18821882
} else {
18831883

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

@@ -2040,6 +2040,13 @@ $.extend( Datepicker.prototype, {
20402040
date.setFullYear( date.getFullYear() - 1900 );
20412041
}
20422042
return date;
2043+
},
2044+
2045+
/* Add leading zeros to produce an at-least-four-digit year. */
2046+
_formatYear: function (year) {
2047+
var yearString = "" + year;
2048+
return year < 0 ? yearString :
2049+
yearString.length < 4 ? ( "0000" + yearString ).slice( -4 ) : yearString;
20432050
}
20442051
} );
20452052

0 commit comments

Comments
 (0)