.datepicker('setdate') issues, in jQuery

Derek Adair picture Derek Adair · Dec 23, 2009 · Viewed 142.5k times · Source

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

Answer

jurka picture jurka · May 19, 2011

When you trying to call setDate you must provide valid javascript Date object.

queryDate = '2009-11-01';

var parsedDate = $.datepicker.parseDate('yy-mm-dd', queryDate);

$('#datePicker').datepicker('setDate', parsedDate);

This will allow you to use different formats for query date and string date representation in datepicker. This approach is very helpful when you create multilingual site. Another helpful function is formatDate, which formats javascript date object to string.

$.datepicker.formatDate( format, date, settings );