Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.datepicker('setdate') issues, in jQuery

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

like image 978
Derek Adair Avatar asked Dec 23 '09 16:12

Derek Adair


People also ask

How do I fix my datepicker position?

To change the position of the jQuery UI Datepicker just modify . ui-datepicker in the css file. The size of the Datepicker can also be changed in this way, just adjust the font size.

What is datepicker in jQuery?

Advertisements. Datepickers in jQueryUI allow users to enter dates easily and visually. You can customize the date format and language, restrict the selectable date ranges and add in buttons and other navigation options easily.

Why datepicker is not working in HTML?

You need to include jQuery, and include it before jQuery UI. You also need to move your code to the end of your document or wrap it in a document ready call. Include a reference to jQuery before jQuery UI. Thanks for the response .


2 Answers

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 ); 
like image 159
jurka Avatar answered Oct 07 '22 11:10

jurka


If you would like to support really old browsers you should parse the date string, since using the ISO8601 date format with the Date constructor is not supported pre IE9:

var queryDate = '2009-11-01',     dateParts = queryDate.match(/(\d+)/g)     realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);                                       // months are 0-based! // For >= IE9 var realDate = new Date('2009-11-01');    $('#datePicker').datepicker({ dateFormat: 'yy-mm-dd' }); // format to show $('#datePicker').datepicker('setDate', realDate); 

Check the above example here.

like image 30
Christian C. Salvadó Avatar answered Oct 07 '22 11:10

Christian C. Salvadó