Skip to content
Permalink
Browse files

Datepicker: default, min, and max dates can be specified as date stri…

…ngs in the current format
  • Loading branch information
Keith Wood
Keith Wood committed Jun 16, 2009
1 parent 37f4f74 commit f07a17bbf0e10190ae742dd8021550a7c57c082e
Showing with 30 additions and 12 deletions.
  1. +15 −3 tests/unit/datepicker/datepicker_options.js
  2. +15 −9 ui/ui.datepicker.js
@@ -142,7 +142,7 @@ test('defaultDate', function() {
inp.val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDate(inp.datepicker('getDate'), date, 'Default date null');
// numeric values
// Numeric values
inp.datepicker('option', {defaultDate: -2}).
datepicker('hide').val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER});
@@ -162,7 +162,7 @@ test('defaultDate', function() {
datepicker('hide').val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDate(inp.datepicker('getDate'), date, 'Default date NaN');
// string values
// String offset values
inp.datepicker('option', {defaultDate: '-1d'}).
datepicker('hide').val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER});
@@ -211,8 +211,20 @@ test('defaultDate', function() {
date = addMonths(new Date(), 1);
date.setDate(date.getDate() + 10);
equalsDate(inp.datepicker('getDate'), date, 'Default date +1M +10d');
// String date values
inp.datepicker('option', {defaultDate: '07/04/2007'}).
datepicker('hide').val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER});
date = new Date(2007, 7 - 1, 4);
equalsDate(inp.datepicker('getDate'), date, 'Default date 07/04/2007');
inp.datepicker('option', {dateFormat: 'yy-mm-dd', defaultDate: '2007-04-02'}).
datepicker('hide').val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER});
date = new Date(2007, 4 - 1, 2);
equalsDate(inp.datepicker('getDate'), date, 'Default date 2007-04-02');
// Date value
date = new Date(2007, 1 - 1, 26);
inp.datepicker('option', {defaultDate: date}).
inp.datepicker('option', {dateFormat: 'mm/dd/yy', defaultDate: date}).
datepicker('hide').val('').datepicker('show').
simulate('keydown', {keyCode: $.simulate.VK_ENTER});
equalsDate(inp.datepicker('getDate'), date, 'Default date 01/26/2007');
@@ -1178,17 +1178,24 @@ $.extend(Datepicker.prototype, {
/* Retrieve the default date shown on opening. */
_getDefaultDate: function(inst) {
return this._restrictMinMax(inst,
this._determineDate(this._get(inst, 'defaultDate'), new Date()));
this._determineDate(inst, this._get(inst, 'defaultDate'), new Date()));
},

/* A date may be specified as an exact value or a relative one. */
_determineDate: function(date, defaultDate) {
_determineDate: function(inst, date, defaultDate) {
var offsetNumeric = function(offset) {
var date = new Date();
date.setDate(date.getDate() + offset);
return date;
};
var offsetString = function(offset, getDaysInMonth) {
var offsetString = function(offset) {
try {
return $.datepicker.parseDate($.datepicker._get(inst, 'dateFormat'),
offset, $.datepicker._getFormatConfig(inst));
}
catch (e) {
// Ignore
}
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
@@ -1203,19 +1210,18 @@ $.extend(Datepicker.prototype, {
day += parseInt(matches[1],10) * 7; break;
case 'm' : case 'M' :
month += parseInt(matches[1],10);
day = Math.min(day, getDaysInMonth(year, month));
day = Math.min(day, $.datepicker._getDaysInMonth(year, month));
break;
case 'y': case 'Y' :
year += parseInt(matches[1],10);
day = Math.min(day, getDaysInMonth(year, month));
day = Math.min(day, $.datepicker._getDaysInMonth(year, month));
break;
}
matches = pattern.exec(offset);
}
return new Date(year, month, day);
};
date = (date == null ? defaultDate :
(typeof date == 'string' ? offsetString(date, this._getDaysInMonth) :
date = (date == null ? defaultDate : (typeof date == 'string' ? offsetString(date) :
(typeof date == 'number' ? (isNaN(date) ? defaultDate : offsetNumeric(date)) : date)));
date = (date && date.toString() == 'Invalid Date' ? defaultDate : date);
if (date) {
@@ -1244,7 +1250,7 @@ $.extend(Datepicker.prototype, {
var clear = !(date);
var origMonth = inst.selectedMonth;
var origYear = inst.selectedYear;
date = this._restrictMinMax(inst, this._determineDate(date, new Date()));
date = this._restrictMinMax(inst, this._determineDate(inst, date, new Date()));
inst.selectedDay = inst.currentDay = date.getDate();
inst.drawMonth = inst.selectedMonth = inst.currentMonth = date.getMonth();
inst.drawYear = inst.selectedYear = inst.currentYear = date.getFullYear();
@@ -1529,7 +1535,7 @@ $.extend(Datepicker.prototype, {

/* Determine the current maximum date - ensure no time components are set. */
_getMinMaxDate: function(inst, minMax) {
return this._determineDate(this._get(inst, minMax + 'Date'), null);
return this._determineDate(inst, this._get(inst, minMax + 'Date'), null);
},

/* Find the number of days in a given month. */

0 comments on commit f07a17b

Please sign in to comment.
You can’t perform that action at this time.