]> source.dussan.org Git - jquery-ui.git/commitdiff
Datepicker: default, min, and max dates can be specified as date strings in the curre...
authorKeith Wood <kbwood.au@gmail.com>
Tue, 16 Jun 2009 08:44:56 +0000 (08:44 +0000)
committerKeith Wood <kbwood.au@gmail.com>
Tue, 16 Jun 2009 08:44:56 +0000 (08:44 +0000)
tests/unit/datepicker/datepicker_options.js
ui/ui.datepicker.js

index 52841468d1519f7aba75f280b7e0563681af2332..8bde84473266e1212261bf116a851d760c712af5 100644 (file)
@@ -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');
index 6dc6989f9ecdd515f3f383d3d0eca914ae70f67a..478b756e384ba7cda1942d744cc503feca013180 100644 (file)
@@ -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. */