Setting another month as default in jQuery UI datepicker before display

蹲街弑〆低调 提交于 2019-12-21 08:18:09

问题


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!


回答1:


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.




回答2:


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.




回答3:


Try this

$( ".selector" ).datepicker({ defaultDate: +7 });

Specify either an actual date via a Date object or as a string in the current dateFormat, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null for today.



来源:https://stackoverflow.com/questions/4170880/setting-another-month-as-default-in-jquery-ui-datepicker-before-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!