Fahad Khan
Fahad Khan

Reputation: 1635

setDate not showing correct month - jQuery Datepicker

setting default date does not showing the month I set, but the month +1

var defaultDateA1 = new Date(2014,7,1) 
$('#DRdatepicker').datepicker();
$('#DRdatepicker').datepicker('setDate', defaultDateA1);

//whole code below in not working as well
$( "#DRdatepicker" ).datepicker({
    readonly: true,
    //setDate: new Date(2014,7,1),  // First tried here, didn't work
    dateFormat: 'yy-mm-dd',
    autoSize: true
});

It is showing 08/01/2014

Upvotes: 1

Views: 524

Answers (2)

Satpal
Satpal

Reputation: 133403

Read Docs

month: Integer value representing the month, beginning with 0 for January to 11 for December.

So you have to use

var defaultDateA1 = new Date(2014,7 - 1, 1) 

With Date format:

$("#DRdatepicker").datepicker({
    readonly: true,    
    dateFormat: 'yy-mm-dd',
    autoSize: true
}).datepicker('setDate', new Date(2014, 7 - 1, 1));

DEMO

Upvotes: 2

Amit
Amit

Reputation: 15387

You need to set date format as below :

 var defaultDateA1 = new Date(2014,7 - 1, 1) 
 var pickerOpts = {
            dateFormat:"y-mm-dd"

        };  
$('#DRdatepicker').datepicker(pickerOpts);  
$('#DRdatepicker').datepicker('setDate', defaultDateA1);

Demo

Upvotes: 1

Related Questions