Reputation: 354
I'm trying to use jQuery UI datepicker in my project and it seems that I can't set date at render time in format as "MM yy"
. But I still can change it in onClose
method after page is loaded. If I change initial date format to "yy-mm-dd"
initial date is set correctly.
Example is here:
http://jsfiddle.net/DBpJe/1446/
In this example if you change dateFormat
to "yy-mm-dd"
then the date is set correctly to a value of realDate
variable. If dateFormat
is set to "MM yy"
, the date is set to current date.
I appreciate any help.
Upvotes: 1
Views: 11226
Reputation: 13351
try setting date at the end instead of in beginning,here is working demo http://jsfiddle.net/DBpJe/1446/
$(function() {
var queryDate = '2009-11-01',
dateParts = queryDate.match(/(\d+)/g)
realDate = new Date(dateParts[0], dateParts[1] - 1, dateParts[2]);
// months are 0-based!
$('#startDate').datepicker({
dateFormat: "MM yy"
}) // format to show
.datepicker("option", "changeMonth", true)
.datepicker("option", "changeYear", true)
.datepicker("option", "showButtonPanel", true)
.datepicker("option", "onClose", function(e){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker("setDate",new Date(year,month,1));
}).datepicker('setDate',realDate);
});
Upvotes: 4