Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting another month as default in jQuery UI datepicker before display

In my situation, I have to set the month to last month as default.

The default month for the datepicker is current month. But I want it to be last month or other month as default showing. How can I make it?

Such as: "2010-09" as the default.

Thank you very much!

like image 699
qinHaiXiang Avatar asked Nov 13 '10 03:11

qinHaiXiang


2 Answers

You can use the defaultDate option when creating the datepicker:

$('#date').datepicker({
    defaultDate: '-2m'
});

By passing in a string like this we can set the default date to another one relative to the current date. Alternatively, the option also accepts a Date object:

defaultDate: new Date(2010, 8, 1)

or a string in the same format as the format currently defined:

defaultDate: '1/9/2010'

All of the above will give you a default date in September. The month in the Date constructor starts from zero, so 8 will give you September.

like image 155
Yi Jiang Avatar answered Oct 08 '22 02:10

Yi Jiang


I think you actually want this when you're only showing 1 month at a time.

$( ".selector" ).datepicker({ showCurrentAtPos: 1 });

If you use defaultDate it will highlight that particular day, this method just shows the previous month with nothing selected.

like image 20
toxaq Avatar answered Oct 08 '22 02:10

toxaq