Skip to content

Commit 10972be

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

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
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" );

tests/unit/datepicker/options.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,9 @@ QUnit.test( "setDate", function( assert ) {
581581
date1.setDate( date1.getDate() - 21 );
582582
inp.datepicker( "setDate", "c -3 w" );
583583
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date1, "Set date - c -3 w" );
584-
date3 = new Date(date1);
584+
date3 = new Date( date1 );
585585
date3.setFullYear( 1 );
586-
inp.datepicker( "setDate", "c " + (1 - date1.getFullYear()) + " y" );
586+
inp.datepicker( "setDate", "c " + ( 1 - date1.getFullYear() ) + " y" );
587587
testHelper.equalsDate( assert, inp.datepicker( "getDate" ), date3, "Set date - 0001 relatively" );
588588

589589
// Inline
@@ -1113,7 +1113,7 @@ QUnit.test( "formatDate", function( assert ) {
11131113
new Date( 2001, 2 - 1, 3 ) ), "day 3 of February ('Saturday'), 2001",
11141114
"Format date 'day' d 'of' MM ('DD'), yy" );
11151115
assert.equal( $.datepicker.formatDate( "yy-mm-dd", $.datepicker._newDate( 999, 2 - 1, 3 ) ),
1116-
"0999-02-03", "Format ancient date yy-mm-dd");
1116+
"0999-02-03", "Format ancient date yy-mm-dd" );
11171117
gmtDate = new Date( 2001, 2 - 1, 3 );
11181118
gmtDate.setMinutes( gmtDate.getMinutes() - gmtDate.getTimezoneOffset() );
11191119
assert.equal( $.datepicker.formatDate( "@", gmtDate ), "981158400000", "Format date @" );

ui/widgets/datepicker.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ $.extend( Datepicker.prototype, {
11731173
size = ( match === "@" ? 14 : ( match === "!" ? 20 :
11741174
( match === "y" && isDoubled ? 4 : ( match === "o" ? 3 : 2 ) ) ) ),
11751175
minSize = ( match === "y" ? size : 1 ),
1176-
digits = new RegExp( "^" + (match === "@" ? "-?" : "") + "\\d{" + minSize + "," + size + "}" ),
1176+
digits = new RegExp( "^" + ( match === "@" ? "-?" : "" ) + "\\d{" + minSize + "," + size + "}" ),
11771177
num = value.substring( iValue ).match( digits );
11781178
if ( !num ) {
11791179
throw "Missing number at position " + iValue;
@@ -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)