views:

71

answers:

1

Once I have set the minDate property of a datepicker with the convenient string syntax

$(elem).datepicker('option','minDate','+1d +3m'); 

how can I get the date object of the minDate? To help illustrate, there is a method

 $(elem).datepicker('getDate');

which returns the date that is entered in the input in the format of a date object. I would like the same thing but for datepicker('getMinDate'). There is an option like this

$(elem).datepicker('option','minDate');

but this returns '+1d +3m' which is not helpful. I need the actual date object to compare with another date object. Any ideas?

+2  A: 

jQuery uses its _determineDate() function to calculate the minDate date object based on its attribute. I modified its behaviour and made a function. Note that it only deals with the "offset" type of values and nothing else.

/* minDateAttr is the minDate option of the datepicker, eg '+1d +3m' */
function getMinDate(minDateAttr) {
    var minDate = new Date();
    var pattern = /([+-]?[0-9]+)\s*(d|D|w|W|m|M|y|Y)?/g;
    var matches = pattern.exec(minDateAttr);
    while (matches) {
        switch (matches[2] || 'd') {
            case 'd' : case 'D' :
                minDate.setDate(minDate.getDate() + parseInt(matches[1],10));
                break;
            case 'w' : case 'W' :
                minDate.setDate(minDate.getDate() + parseInt(matches[1],10) * 7);
                break;
            case 'm' : case 'M' :
                minDate.setMonth(minDate.getMonth() + parseInt(matches[1],10));
                break;
            case 'y': case 'Y' :
                minDate.setYear(minDate.getFullYear() + parseInt(matches[1],10));
                break;
        }
        matches = pattern.exec(minDateAttr);
    }
    return minDate;
}


I originally planned on answering the following, but came up with a (better) solution - the one above. However, I'm going to include it, in case it's needed for debugging reasons etc.

The _determineDate() function is technically availible for use, but it's not supposed to be used and may change in the future. Nevertheless, this would be how to use it:

var minDateAttr = $(elem).datepicker("option", "minDate");
var inst = $(elem).data("datepicker");
var minDateObj = $.datepicker._determineDate(inst, minDateAttr, new Date());
Simen Echholt
Wow, Stackoverflow, you never cease to amaze me. This is great! I would prefer the first solution. Hate breaking away from framework update paths :).
Adrian Adkison