Ticket #6872 (closed bug: fixed)

Opened 4 years ago

Last modified 4 years ago

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:
Blocking: Blocked by:

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).

Change History

comment:1 Changed 4 years ago by Ivan Peters

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

comment:2 Changed 4 years ago by Ivan Peters

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.

comment:3 Changed 4 years ago by Ivan Peters

comment:4 Changed 4 years ago by Ivan Peters

  • Status changed from new to closed
  • Resolution set to fixed

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

Changeset: 015ea163d1995f5a3f42fd394d9db3abdc0fa16a

comment:5 Changed 4 years ago by Ivan Peters

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

Changeset: 67b070f97a6dc4907cbb5e69b8899c0b5c716684

comment:6 Changed 4 years ago by scott.gonzalez

  • Milestone changed from 1.9 to 1.8.9
Note: See TracTickets for help on using tickets.