views:

898

answers:

4

Hi,

I have a function that executes a query to get some data based on a certain date range which is selected using .datepicker(). I am trying to set the datepicker's that are in the response data back to the date range which was selected before the query was executed.

queryDate = '2009-11-01';
$('#datePicker').datepicker('setDate', queryDate);

has the interesting result of setting the datepicker to today's date! I wouldn't be so confused if it just spit out an error. Why does it set it to today's date?

How can I take the date which is formated like, 'yyyy-mm-dd' and set the datepicker to it?

I am thinking it might be best to just set the text-box which the datepicker is linked to to 'queryDate'

-thanks

+1  A: 

Try changing it to:

queryDate = '2009-11-01';
$('#datePicker').datepicker({defaultDate: new Date (queryDate)});
Scobal
+1  A: 

As Scobal's post implies, the datepicker is looking for a Date object - not just a string! So, to modify your example code to do what you want:

var queryDate = new Date('2009/11/01'); // Dashes won't work
$('#datePicker').datepicker('setDate', queryDate);
Matt Ball
+2  A: 

You should parse the date string, since using the ISO8601 date format with the Date constructor is not supported cross-browserly yet, (some browsers support it since it will be part of the EcmaScript 5 standard):

var queryDate = '2009-11-01',
    dateParts = queryDate.match(/(\d+)/g)
    realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);  
                                    // months are 0-based!

$('#datePicker').datepicker({ dateFormat: 'yy-mm-dd' }); // format to show
$('#datePicker').datepicker('setDate', realDate);

Check the above example here.

CMS
You missed a bit: `$('#datePicker').datepicker('setDate', realDate);`
Matt Ball
Thanks!, fixed...
CMS
+1  A: 

Check that the date you are trying to set it to lies within the allowed date range if the minDate or maxDate options are set.

cfwall