Skip to main content

Search and Top Navigation

#6872 closed bug (fixed)

Opened January 17, 2011 10:28PM UTC

Closed January 18, 2011 05:42AM UTC

Last modified January 18, 2011 05:43AM UTC

datepicker: parseDate does not default shortYearCutoff correctly

Reported by: Ivan Peters Owned by:
Priority: minor Milestone: 1.8.9
Component: ui.datepicker Version: 1.8.8
Keywords: Cc:
Blocked by: Blocking:
Description

The following code results in a date in 1911:

$.datepicker.parseDate("d/m/y","5/1/11")

Test case: http://jsbin.com/emifa4/6/

The problem is that a settings parameter hasn't been provided and parseDate is not correctly determining the default. It does this:

var shortYearCutoff = (settings ? settings.shortYearCutoff : null) || this._defaults.shortYearCutoff;

This results in a default value of "+10". When it is later used here:

year += new Date().getFullYear() - new Date().getFullYear() % 100 + (year <= shortYearCutoff ? 0 : -100);

the "+10" resolves to a cutoff value of 10 instead of the current year plus 10 that it is supposed to indicate. This is likely to become a more noticeable issue now that the year is 2011.

The quick fix is to update the initialization to this:

var shortYearCutoff = (settings ? settings.shortYearCutoff : null) || this._defaults.shortYearCutoff;
		shortYearCutoff = (typeof shortYearCutoff != 'string' ? shortYearCutoff :
			new Date().getFullYear() % 100 + parseInt(shortYearCutoff, 10));

This is reproducing what is done in _getFormatConfig. Rather than duplication code, maybe _get should handle the lack of an inst value by returning a default. Then parseDate can just call _getFormatConfig if it is not given a settings parameter.

The current _get would be changed from this:

_get: function(inst, name) {
	return inst.settings[name] !== undefined ?
		inst.settings[name] : this._defaults[name];
},

to this:

_get: function(inst, name) {
	return (inst && inst.settings && inst.settings[name] !== undefined) ?
		inst.settings[name] : this._defaults[name];
},

Then the following lines in parseDate:

var shortYearCutoff = (settings ? settings.shortYearCutoff : null) || this._defaults.shortYearCutoff;
var dayNamesShort = (settings ? settings.dayNamesShort : null) || this._defaults.dayNamesShort;
var dayNames = (settings ? settings.dayNames : null) || this._defaults.dayNames;
var monthNamesShort = (settings ? settings.monthNamesShort : null) || this._defaults.monthNamesShort;
var monthNames = (settings ? settings.monthNames : null) || this._defaults.monthNames;

could be replaced with:

settings = $.extend( this._getFormatConfig( ), ( settings || {} ) );

var shortYearCutoff = settings.shortYearCutoff, dayNamesShort = settings.dayNamesShort,
	dayNames = settings.dayNames, monthNamesShort = settings.monthNamesShort, monthNames = monthNames;

(as I think each variable only gets used once, the variables could get dropped altogether).

Attachments (0)
Change History (6)

Changed January 17, 2011 10:33PM UTC by Ivan Peters comment:1

If I get some feedback on the suggested fixes, I'll make the changes on GitHub.

Changed January 18, 2011 01:18AM UTC by Ivan Peters comment:2

OK, I probably shouldn't have thought up a solution and written it here without testing. The second suggested fix doesn't handle the situation where a string value is passed into parseDate in settings.shortYearCutoff. Rewriting _getFormatConfig to accept a settings object as an optional second parameter but that is possibly getting beyond the scope of a bugfix like this (especially considering there is a complete rewrite in the works). So maybe the first option is the way to go for now.

Changed January 18, 2011 03:36AM UTC by Ivan Peters comment:3

Changed January 18, 2011 05:42AM UTC by Ivan Peters comment:4

resolution: → fixed
status: newclosed

Datepicker: Update parseDate to properly handle a string value in shortYearCutoff. Fixed #6872: parseDate does not default shortYearCutoff correctly.

Changeset: 015ea163d1995f5a3f42fd394d9db3abdc0fa16a

Changed January 18, 2011 05:42AM UTC by Ivan Peters comment:5

Datepicker: Update parseDate to properly handle a string value in shortYearCutoff. Fixed #6872: parseDate does not default shortYearCutoff correctly.

Changeset: 67b070f97a6dc4907cbb5e69b8899c0b5c716684

Changed January 18, 2011 05:43AM UTC by scottgonzalez comment:6

milestone: 1.91.8.9