Skip to content
This repository was archived by the owner on Aug 1, 2024. It is now read-only.

Commit fd05d94

Browse files
committed
Fix initializing of date with year < 100 in datepicker.
RELNOTES: Fix initializing of date with year < 100 in datepicker. Before the fix: If you init the datepicker with date with year < 100 (eg.23) it will look like it works, but clicking on the grid cell will switch it 1900+ years (eg. 1923). After the fix: The year < 100 is preserved through the interaction with the control. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=125713669
1 parent 8b9439e commit fd05d94

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

closure/goog/ui/datepicker.js

+11
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,8 @@ goog.ui.DatePicker.prototype.setDate_ = function(date, fireSelection) {
785785
// Set current month
786786
if (date) {
787787
this.activeMonth_.set(this.date_);
788+
// Set years with two digits to their full year, not 19XX.
789+
this.activeMonth_.setFullYear(this.date_.getFullYear());
788790
this.activeMonth_.setDate(1);
789791
}
790792

@@ -1392,7 +1394,16 @@ goog.ui.DatePicker.prototype.updateCalendarGrid_ = function() {
13921394
this.grid_[y] = [];
13931395
for (var x = 0; x < 7; x++) { // Weekdays
13941396
this.grid_[y][x] = date.clone();
1397+
// Date.add breaks dates before year 100 by adding 1900 to the year
1398+
// value. As a workaround we store the year before the add and reapply it
1399+
// after (with special handling for January 1st).
1400+
var year = date.getFullYear();
13951401
date.add(dayInterval);
1402+
if (date.getMonth() == 0 && date.getDate() == 1) {
1403+
// Increase year on January 1st.
1404+
year++;
1405+
}
1406+
date.setFullYear(year);
13961407
}
13971408
}
13981409

closure/goog/ui/datepicker_test.js

+84
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,25 @@ function testGetActiveMonth() {
163163
picker.getActiveMonth());
164164
}
165165

166+
function testGetActiveMonthBeforeYear100() {
167+
var date = new Date(23, 5, 5);
168+
// Above statement will create date with year 1923, need to set full year
169+
// explicitly.
170+
date.setFullYear(23);
171+
172+
var expectedMonth = new goog.date.Date(23, 5, 1);
173+
expectedMonth.setFullYear(23);
174+
175+
picker = new goog.ui.DatePicker(date);
176+
var month = picker.getActiveMonth();
177+
assertObjectEquals(expectedMonth, month);
178+
179+
month.setMonth(10);
180+
assertObjectEquals(
181+
'modifying the returned object is safe', expectedMonth,
182+
picker.getActiveMonth());
183+
}
184+
166185
function testGetDate() {
167186
picker = new goog.ui.DatePicker(new Date(2000, 0, 1));
168187
var date = picker.getDate();
@@ -177,6 +196,71 @@ function testGetDate() {
177196
assertNull('no date is selected', picker.getDate());
178197
}
179198

199+
function testGetDateBeforeYear100() {
200+
var inputDate = new Date(23, 5, 5);
201+
// Above statement will create date with year 1923, need to set full year
202+
// explicitly.
203+
inputDate.setFullYear(23);
204+
picker = new goog.ui.DatePicker(inputDate);
205+
var date = picker.getDate();
206+
207+
var expectedDate = new goog.date.Date(23, 5, 5);
208+
expectedDate.setFullYear(23);
209+
assertObjectEquals(expectedDate, date);
210+
211+
picker.setDate(inputDate);
212+
assertObjectEquals(expectedDate, picker.getDate());
213+
var expectedMonth = new goog.date.Date(23, 5, 1);
214+
expectedMonth.setFullYear(23);
215+
assertObjectEquals(expectedMonth, picker.getActiveMonth());
216+
}
217+
218+
function testGridForDecember23() {
219+
// Initialize picker to December 23.
220+
var inputDate = new Date(23, 11, 5);
221+
// Above statement will create date with year 1923, need to set full year
222+
// explicitly.
223+
inputDate.setFullYear(23);
224+
picker = new goog.ui.DatePicker(inputDate);
225+
picker.create(sandbox);
226+
227+
// Grid start with last days of November 23, shows December 23 and first days
228+
// of January 24.
229+
for (var i = 0; i < 6; i++) {
230+
for (var j = 0; j < 7; j++) {
231+
var date = picker.getDateAt(i, j);
232+
if (date.getMonth() == 0) {
233+
assertEquals(24, date.getFullYear());
234+
} else {
235+
assertEquals(23, date.getFullYear());
236+
}
237+
}
238+
}
239+
}
240+
241+
function testGridForJanuary22() {
242+
// Initialize picker to January 22.
243+
var inputDate = new Date(22, 0, 5);
244+
// Above statement will create date with year 1922, need to set full year
245+
// explicitly.
246+
inputDate.setFullYear(22);
247+
picker = new goog.ui.DatePicker(inputDate);
248+
picker.create(sandbox);
249+
250+
// Grid start with last days of December 21, shows January 22 and first days
251+
// of February 22.
252+
for (var i = 0; i < 6; i++) {
253+
for (var j = 0; j < 7; j++) {
254+
var date = picker.getDateAt(i, j);
255+
if (date.getMonth() == 11) {
256+
assertEquals(21, date.getFullYear());
257+
} else {
258+
assertEquals(22, date.getFullYear());
259+
}
260+
}
261+
}
262+
}
263+
180264
function testGetDateAt() {
181265
picker = new goog.ui.DatePicker();
182266
picker.create(sandbox);

0 commit comments

Comments
 (0)